Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.