Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Share useState with useContext
import React, { createContext, useState, useContext, FC } from 'react';
type ContextValue = [boolean, (value: boolean) => void] | undefined;
const FullwidthContext = createContext<ContextValue>(undefined);
const defaultState = false;
export const FullwidthProvider: FC = ({ children }) => {
const state = useState(defaultState);
return (
<FullwidthContext.Provider value={state}>
{children}
</FullwidthContext.Provider>
);
};
export function useFullwidth() {
const context = useContext(FullwidthContext);
if (!context)
throw new Error('useFullwidth must be used within a FullwidthProvider.');
return context;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.