Home > Web Front-end > JS Tutorial > Source code analysis of React-redux (code)

Source code analysis of React-redux (code)

不言
Release: 2018-09-14 15:36:51
Original
1136 people have browsed it

This article brings you the source code analysis (code) of React-redux. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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 = {
Copy after login
             [storeKey]: storeShape.isRequired,
            [subscriptionKey]: subscriptionShape,
        }
    
            return Provider
    }
Copy after login

The usual approach is that we first create the store through redux, and then assign it to the store attribute of the Provider component. Inside the Provider component, get the store and set it to the context attribute, so that all its components can get the store through the context.

   <Provider store={store}>
       <App />
   </Provider>
Copy after login

Related recommendations:

YII source code analysis, YII source code analysis

##Symfony2 source code analysis startup process 2, symfony2 Source code

The above is the detailed content of Source code analysis of React-redux (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template