createContext
createContext
is a helper function that guards against accessing a Context
whose value wasn't provided. By doing this, API instead, we never have to
provide a default and nver have to check for undefined
.
Import#
import { createContext } from '@dwarvesf/react-utils'
Usage#
const [CurrentUserProvider, useCurrentUserName] = createContext<string>({});function EnthusasticGreeting() {const currentUser = useCurrentUserName();return <div>HELLO {currentUser.toUpperCase()}!</div>;}function App() {return (<CurrentUserProvider value="Anders"><EnthusasticGreeting /></CurrentUserProvider>);}