Skip to content

Instantly share code, notes, and snippets.

@Jahans3
Last active November 30, 2018 03:25
Show Gist options
  • Save Jahans3/7019ec9bdcfa41228e4ced4f99a4821d to your computer and use it in GitHub Desktop.
Save Jahans3/7019ec9bdcfa41228e4ced4f99a4821d to your computer and use it in GitHub Desktop.
Dispatch to reducers
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(action, state) || 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