Skip to content

Instantly share code, notes, and snippets.

@jeffscottward
Created July 30, 2019 02:58
Show Gist options
  • Save jeffscottward/3669146e8cc491825a75510edae048f1 to your computer and use it in GitHub Desktop.
Save jeffscottward/3669146e8cc491825a75510edae048f1 to your computer and use it in GitHub Desktop.
State Management in 10 lines w/ React Context & Hooks
// From https://bit.ly/2XH31rw
import React, { createContext, useContext, useReducer } from "react";
export const StateContext = createContext();
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
export const useStateValue = () => useContext(StateContext);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment