Skip to content

Instantly share code, notes, and snippets.

@Jahans3
Last active December 1, 2018 17:48
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 Jahans3/68e4b0e6c68b392a4e5751ad98d16666 to your computer and use it in GitHub Desktop.
Save Jahans3/68e4b0e6c68b392a4e5751ad98d16666 to your computer and use it in GitHub Desktop.
Use reducer
export function StateProvider ({ state: initialState, reducers, middleware, children }) {
const [state, dispatch] = useReducer((state, action) => {
return reducers.reduce((state, reducer) => reducer(state, action) || state, state);
}, initialState);
return (
<StateContext.Provider value={{ state, dispatch }}>
{children}
</StateContext.Provider>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment