react-redux 理解第一篇Provider 组件

2018-12-25  本文已影响0人  happy_milo

先看一下原码

let didWarnAboutReceivingStore = false
export default class Provider extends Component {
  getChildContext() {
    return { store: this.store }
  }
  constructor(props, context) {
    super(props, context)
    this.store = props.store
  }
  render() {
    return Children.only(this.props.children)
  }
}

if (process.env.NODE_ENV !== 'production') {
  Provider.prototype.componentWillReceiveProps = function (nextProps) {
    const { store } = this
    const { store: nextStore } = nextProps

    if (store !== nextStore) {
      warnAboutReceivingStore()
    }
  }
}

Provider.displayName = 'Provider'

Provider 组件可以说是非常简单只做了三件事

上一篇下一篇

猜你喜欢

热点阅读