provider
//最后导出的是createprovider()。所以一开始storekey应该是以默认值‘store’传进去的 function createprovider(storekey = 'store', subkey) { const subscriptionkey = subkey || `${storekey}subscription` class provider extends component { //设置context,能让子组件拿到store //相当于返回 {store: this.store} getchildcontext() { return { [storekey]: this[storekey], [subscriptionkey]: null } } constructor(props, context) { super(props, context) //this.store = props.store this[storekey] = props.store; } render() { //只能有一个子组件 return children.only(this.props.children) } } //props和context类型验证 provider.proptypes = { store: storeshape.isrequired, children: proptypes.element.isrequired, } provider.childcontexttypes = {
[storekey]: storeshape.isrequired, [subscriptionkey]: subscriptionshape, } return provider }
通常的做法是我们先通过redux创建好store,然后赋给provider组件的store属性。在provider组件内部,拿到这个store,设置为context属性,这样就能让它的所有组件都能通过context拿到store。
<provider store={store}> <app /> </provider>
相关推荐:
yii 的源码分析,yii源码分析
symfony2源码分析启动过程2,symfony2源码
以上就是对react-redux的源码分析(代码)的详细内容。
