日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

redux provider源码解析

發(fā)布時間:2024/9/21 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redux provider源码解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

provider 源碼

import { Component, Children } from 'react' import PropTypes from 'prop-types' import storeShape from '../utils/storeShape' import warning from '../utils/warning' let didWarnAboutReceivingStore = falsefunction warnAboutReceivingStore() {if (didWarnAboutReceivingStore) {return}didWarnAboutReceivingStore = truewarning('<Provider> does not support changing `store` on the fly. ' +'It is most likely that you see this error because you updated to ' +'Redux 2.x and React Redux 2.x which no longer hot reload reducers ' +'automatically. See https://github.com/reactjs/react-redux/releases/' +'tag/v2.0.0 for the migration instructions.') }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 } = thisconst { store: nextStore } = nextPropsif (store !== nextStore) {warnAboutReceivingStore()}} }Provider.propTypes = {store: storeShape.isRequired,children: PropTypes.element.isRequired } Provider.childContextTypes = {store: storeShape.isRequired }

首先學習react數據傳遞三種方法,props,state,context

props:一般用于將父組件數據傳遞到子組件,不能跨組件傳遞

state:state是內部狀態(tài)或者局部狀態(tài)
? ? ? ? ? ?es6數據解析操作,let {xxx ,xx, x} = this.state;

context: 和props相比,context可以跨組件傳遞信息

聲明context步驟一:

constructor(props, context) {super(props, context)this.store = props.store}getChildContext() {return { store: this.store }}

更具react生命周期,首先constructor方法獲取屬性store,getChildContext()將store放入context中

步驟二:

Provider.propTypes = {store: storeShape.isRequired,children: PropTypes.element.isRequired }

驗證組件信息

步驟三:

Provider.childContextTypes = {store: storeShape.isRequired }

聲明了childrenContextTypes。如果不聲明的話,將無法在組件中使用getChildContext()方法;

獲取context:

子樹中的任何組件都可以通過定義ContextTypes?去訪問它。

connect中通過

? Connect.contextTypes = {store: storeShape}

然后通過constructor獲取store?

constructor(props, context) {super(props, context)this.version = versionthis.store = props.store || context.storeconst storeState = this.store.getState()this.state = { storeState }this.clearCache()}

最后執(zhí)行render渲染,返回一個react子元素。Children.only是react提供的方法,this.props.children表示的是只有一個root的元素。

轉載于:https://my.oschina.net/u/3647713/blog/1535249

總結

以上是生活随笔為你收集整理的redux provider源码解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。