Created
April 16, 2019 09:11
-
-
Save MeetMartin/14e1d80195ef38caf8640fe5f7f8bf11 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// src/store/StoreContext.js | |
import React, { createContext, useReducer } from 'react'; | |
import { reducer, initialState } from './reducers'; | |
import { useActions } from './actions'; | |
import { applyMiddleware } from './middleware'; | |
const StoreContext = createContext(initialState); | |
const StoreProvider = ({ children }) => { | |
const [state, dispatch] = useReducer(reducer, initialState); | |
// Attach middleware to capture every dispatch | |
const enhancedDispatch = applyMiddleware(dispatch); | |
const actions = useActions(state, enhancedDispatch); | |
return ( | |
<StoreContext.Provider | |
value={{ state, enhancedDispatch, actions }} | |
> | |
{children} | |
</StoreContext.Provider> | |
); | |
}; | |
export { StoreContext, StoreProvider }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment