手揣网教程:绿色安全纯净官方版,体验当今速度最快的浏览器!

对React-redux的源码区分(代码)

时间:2024/12/6作者:未知来源:手揣网教程人气:

[摘要]本篇文章给大家带来的内容是关于React-redux的源码分析(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。Provider //最后导出的是createProvid...
本篇文章给大家带来的内容是关于React-redux的源码分析(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

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的源码分析(代码)的详细内容,更多请关注php中文网其它相关文章!


网站建设是一个广义的术语,涵盖了许多不同的技能和学科中所使用的生产和维护的网站。



关键词:对React-redux的源码区分(代码)




Copyright © 2012-2018 手揣网教程(http://www.shouchuai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版