import createStoreShape from '../utils/createStoreShape';

export default function createProvider(React) {
  const { Component, PropTypes } = React;
  const storeShape = createStoreShape(PropTypes);

  return class Provider extends Component {
    static childContextTypes = {
      store: storeShape.isRequired
    };

    static propTypes = {
      children: PropTypes.func.isRequired
    };

    getChildContext() {
      return { store: this.state.store };
    }

    constructor(props, context) {
      super(props, context);
      this.state = { store: props.store };
    }

    componentWillReceiveProps(nextProps) {
      const { store } = this.state;
      const { store: nextStore } = nextProps;

      if (store !== nextStore) {
        const nextReducer = nextStore.getReducer();
        store.replaceReducer(nextReducer);
      }
    }

    render() {
      const { children } = this.props;
      return children();
    }
  };
}

import createStoreShape from '../utils/createStoreShape';

export default function createProvider(React) {
  const { Component, PropTypes } = React;
  const storeShape = createStoreShape(PropTypes);

  return class Provider extends Component {
    static childContextTypes = {
      store: storeShape.isRequired
    };

    static propTypes = {
      children: PropTypes.func.isRequired,
      store: storeShape.isRequired
    };

    getChildContext() {
      return { store: this.state.store };
    }

    constructor(props, context) {
      super(props, context);
      this.state = { store: props.store };
    }

    componentWillReceiveProps(nextProps) {
      const { store } = this.state;
      const { store: nextStore } = nextProps;

      if (store !== nextStore) {
        const nextReducer = nextStore.getReducer();
        store.replaceReducer(nextReducer);
      }
    }

    render() {
      const { children } = this.props;
      return children();
    }
  };
}

Enforce shape of store property
Enforce shape of store property

This produce better Error messages, when store property is not being provided.

In our case we migrated to redux-1.0.x and got the waring: 
"Warning: Failed Context Types: Required child context `store` was not specified in `Provider`"
because previously the property holding the flux instance was named redux.

With the propTypes specified you would get the following warning: 
"Warning: Failed propType: Required prop `store` was not specified in `Provider`"
which makes it pretty clear how and where to fix the issue.