create-hook-context
A more powerful version of React.createContext
. Accepts a hook that takes in props for a provider, and then returned value of the hook, is the value provided to the context's consumers.
Signature:
import createContext from 'create-hook-context';
const [ThemeProvider, useTheme, withThemeProvider, ThemeContext] = createContext(
({ theme }) => theme,
{},
"Theme"
)
const Consumer = () => {
const val = useTheme();
return <pre>{JSON.stringify(val)}</pre>;
};
const App = () => {
return (
<ThemeProvider theme={{ a: 1 }}>
<Consumer />
</ThemeProvider>
);
};
const App = withThemeProvider({ theme: { a: 1 } }, () => {
const val = useTheme();
return <pre>{JSON.stringify(val)}</pre>;
});