Skip to content

Instantly share code, notes, and snippets.

@Jahans3
Last active December 1, 2018 17:42
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/cc6787f6e31e5d961d74012f5c21083d to your computer and use it in GitHub Desktop.
Save Jahans3/cc6787f6e31e5d961d74012f5c21083d to your computer and use it in GitHub Desktop.
Store with dispatch
export class Provider extends React.PureComponent {
static defaultProps = {
state: {},
reducers: []
};
state = this.props.state;
_dispatch = action => {
const { reducers } = this.props;
const nextState = reducers.reduce((state, reducer) => {
return reducer(state, action) || state;
}, this.state);
this.setState(nextState);
};
render () {
return (
<StateContext.Provider value={{ state: this.state, dispatch: this._dispatch }}>
{this.props.children}
</StateContext.Provider>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment