Created
November 15, 2020 10:41
-
-
Save cosemansp/ec3aad99cfa506e02c85058b9272926f to your computer and use it in GitHub Desktop.
Improved React Context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createContext, useContext, useMemo, ConsumerProps, ReactNode } from 'react' | |
interface Value { | |
mode: 'dark' | 'light'; | |
setMode: (mode: 'dark' | 'light') => void; | |
compact: boolean; | |
setCompact: (compact: boolean) => void; | |
} | |
const Context = createContext<Value | null>(null) | |
export function useTheme(): Value { | |
const value = useContext(Context) | |
if (value === null) throw new Error('no value provided') | |
return value | |
} | |
export function ThemeProvider({ children }: { children: ReactNode }) { | |
const [mode, setMode] = useState<Value['mode']>('dark') | |
const [compact, setCompact] = useState<Value['compact']>(false) | |
const value = useMemo(() => ({ | |
mode, | |
setMode, | |
compact, | |
setCompact, | |
}), [mode, setMode, compact, setCompact]) | |
return <Context.Provider value={value}>{children}</Context.Provider> | |
} | |
// OPTIONAL | |
export function ThemeConsumer({ children }: ConsumerProps<Value>) { | |
const value = useTheme() // will throw if value is null | |
return children(value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment