Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Created April 16, 2019 09:11
Show Gist options
  • Save MeetMartin/14e1d80195ef38caf8640fe5f7f8bf11 to your computer and use it in GitHub Desktop.
Save MeetMartin/14e1d80195ef38caf8640fe5f7f8bf11 to your computer and use it in GitHub Desktop.
// 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