import React from 'react'; export default class StoreProvider extends React.Component { constructor(props) { super(props); this.dispatch = this.createDispatch(props.reducer); this.state = props.reducer(this.state, { type: '@store/init' }); } createDispatch(reducer) { return (action) => { const state = reducer(this.state, action); this.setState(() => state); return state; }; } componentWillReceiveProps({ reducer }) { if (reducer !== this.props.reducer) { this.dispatch = this.createDispatch(reducer); } } render() { return this.props.render({ dispatch: this.dispatch, state: this.state, }); } }