2019独角兽企业重金招聘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是内部状态或者局部状态
           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()}

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

转载于:https://my.oschina.net/u/3647713/blog/1535249

redux provider源码解析相关推荐

  1. Redux 源码解析系列(一) -- Redux的实现思想

    文章来源: IMweb前端社区 黄qiong(imweb.io) IMweb团队正在招聘啦,简历发至jayccchen@tencent.com Redux 其实是用来帮我们管理状态的一个框架,它暴露给 ...

  2. redux 思考以及源码解析

    1. 基本概念 redux有以下几个基本概念: 1.1. action action: 是一个对象,对一个行为的基本描述 {type:'add',todo } 1.2 action creator 一 ...

  3. react redux 简化_Redux 源码解析

    前言 近日,出于对 Redux 的好奇和想对自己用过的东西知根知底之目的,做了一个 Redux 的自我检测,以便彻底了解其精髓之处.下面我会对使用 Redux 之后产生的疑问做一个清单,用问题导向往下 ...

  4. Redux异步解决方案之Redux-Thunk原理及源码解析

    前段时间,我们写了一篇Redux源码分析的文章,也分析了跟React连接的库React-Redux的源码实现.但是在Redux的生态中还有一个很重要的部分没有涉及到,那就是Redux的异步解决方案.本 ...

  5. Redux 异步数据流-- thunk中间件源码解析

    Thunk 引入背景 这是一个关于Redux异步数据流的故事.引入thunk中间件的完整故事在Redux官方中文文档异步数据流.一句话总结就是:原生Redux只支持同步数据流,所以需要引入中间件(mi ...

  6. 【阅读笔记】Taro转小程序编译源码解析

    前言 这篇文章的主要是对taro/taro-tarnsformer-wx进行源码解析,对于想要了解Taro或者了解babel的人希望看了能得到一定的启发. 由于我文笔实在太烂,所以整篇文章都是以阅读笔 ...

  7. React深入学习与源码解析笔记

    ***当前阶段的笔记 *** 「面向实习生阶段」https://www.aliyundrive.com/s/VTME123M4T9 提取码: 8s6v 点击链接保存,或者复制本段内容,打开「阿里云盘」 ...

  8. dubbo源码解析(三十五)集群——cluster

    集群--cluster 目标:介绍dubbo中集群容错的几种模式,介绍dubbo-cluster下support包的源码. 前言 集群容错还是很好理解的,就是当你调用失败的时候所作出的措施.先来看看有 ...

  9. JVM-白话聊一聊JVM类加载和双亲委派机制源码解析

    文章目录 Java 执行代码的大致流程 类加载loadClass的步骤 类加载器和双亲委派机制 sun.misc.Launcher源码解析 Launcher实例化 Launcher 构造函数 双亲委派 ...

最新文章

  1. 【错误记录】Android Gradle 配置报错 ( gradle.properties 配置到 BuildConfig 中需要注意类型转换 | 位置: 类 BuildConfig )
  2. 【Linux】虚拟机 Ubuntu sudo指令实现Gparted安装和 dsv/sda1 内存扩展
  3. 通过实例代码理解WPF的Dispatcher
  4. windows 虚拟地址映射到物理地址
  5. c语言 srand time 0,c++ 随机数 srand(time(0)) 用法 | 求索阁
  6. Android 之视频监控
  7. java 基本功 —— 内存相关
  8. Android查看每个线程CPU占用情况,以及工作内容分析
  9. T0、T1、TS、D1是什么鬼?一文看懂POS机刷卡到账模式区别和利弊
  10. bzoj #1854 游戏(二分图匹配)
  11. 【线性代数之二】矩阵与行列式
  12. 《在职报考英语四六级须知》
  13. 频域处理:傅里叶变换及小波变换
  14. 一台电脑同时安装IE6、IE7、IE8三种浏览器
  15. DFT与DTFT区别
  16. html标签 ppt,HTML框架标签、超链接标签、控件标签.ppt
  17. 静态补充CSS之IFC、FFC、GFC讲解
  18. 韦东山 IMX6ULL和正点原子_stm32入门开发板选野火还是正点原子好,哪个的视频讲到好一点?...
  19. Python数据分析训练营——Python基础语法
  20. 携程旅行android pad客户端,携程发布Android Pad版 无线布局初步完成

热门文章

  1. Boost::Flyweight 基本示例
  2. boost::coroutine模块实现合并数组的测试程序
  3. boost的chrono模块模拟线程接口的测试程序
  4. VTK:可视化之CreateColorSeries
  5. VTK:几何对象之EarthSource
  6. OpenCV FLANN在数据集中搜索查询图片的实例(附完整代码)
  7. C语言删除链表的算法(附完整源码)
  8. C++反转字符串的算法(附完整源码)
  9. C++数组的左右旋转的实现算法(附完整源码)
  10. stl中unordered_map 和 map的区别 ?