Skip to content

Instantly share code, notes, and snippets.

@kirjavascript
Created November 19, 2018 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirjavascript/162add1e0bc5b2ba9adc28712d77983d to your computer and use it in GitHub Desktop.
Save kirjavascript/162add1e0bc5b2ba9adc28712d77983d to your computer and use it in GitHub Desktop.
// custom hook: useStore
const ctx = createContext();
export const Store = ({ children }) => {
const [state, setState] = useState(initialState);
const mergeState = useCallback((obj) => {
setState(state => ({ ...state, ...obj }));
}, []);
return <ctx.Provider
value={{ store: state, setStore: mergeState }}
children={children}
/>
}
export const useStore = () => {
const { store, setStore } = useContext(ctx);
return [store, setStore];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment