Skip to content

Instantly share code, notes, and snippets.

@icarus-sullivan
Created July 4, 2019 17:10
Show Gist options
  • Save icarus-sullivan/50b163ae8e239c0d00a5807a626fdca8 to your computer and use it in GitHub Desktop.
Save icarus-sullivan/50b163ae8e239c0d00a5807a626fdca8 to your computer and use it in GitHub Desktop.
Show the simplicity needed for a contextual HOC
import React, { useContext } from 'react';
import { usePersistState } from 'hooks/usePersistState';
const Context = React.createContext('default');
const Theme = ({ theme, children }) => {
const [_theme, setTheme] = usePersistState({ key: 'theme', defaultValue: theme });
return (
<Context.Provider value={{ theme: _theme, setTheme }}>
{children}
</Context.Provider>
)
};
export const withTheme = (Component) =>
(props) => (<Component {...props} {...useContext(Context)} />);
export default Theme;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment