Created
October 14, 2019 19:55
-
-
Save AlessandroAnnini/c29b38a1c22eaebd5b5578828288dd6c to your computer and use it in GitHub Desktop.
Replace Redux with React Hooks, the easy way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useReducer, createContext } from "react"; | |
export const Store = createContext(); | |
export const createStoreProvider = ({ initialState, reducers }) => ({ | |
children | |
}) => { | |
const reducer = (state, action) => reducers[action.type](state, action); | |
const [state, dispatch] = useReducer(reducer, initialState); | |
return ( | |
<Store.Provider value={{ state, dispatch }}>{children}</Store.Provider> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment