• react 是 view 层的一个框架,负责展示数据;redux 控制数据流动,把数据存在唯一的 store 里,通过 action 来触发事件,reducer 来根据事件处理数据。
  • redux 在通过 reducer 变更完 store tree 后就止步了,它并不能将每次变更之后的 state 及时显示到页面上,所以,为了让组件能够接收到 store,每次 state 更新后及时显示到页面上,我们需要有本文介绍的 react-redux。
  • react-redux 由两部分构成:Provider 和 connect,Provider 负责将 store 传递给内部所有组件,connect 负责注册监听器,在每一次状态变更之后把新的状态和 action 等作为属性挂到容器组件的 this.props 下。

Provider

作用:将 store 通过 context 的方式传递给所有子组件。

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)
}
}

Provider 包裹在组件的最外层,接收一个 store tree,首先在 constructor 里把接收到的属性存到 this.store 里,在 getChildContext 里把 store return 出来,这样包在它里面的组件都可以通过 context 取到 store,将 store 传给了所有的子组件。render 中 Children.only 表示只 render 其中第一个子组件,并且要求组件的第一级子组件只有一个。

connect

作用:在每一次 state 变化之后,用新的 store tree 重新渲染组件。

connect 函数接收 4 个参数:

mapStateToProps

参数为整个 store tree,返回值为需要 merge 进 props 的 state。

mapDispatchToProps

参数为整个 store.dispatch(),返回值为需要 merge 进 props 的 action。

mergeProps

将前面两个函数的返回值,加上自定义的属性,合并到一起,挂到容器组件的 this props 上。

注意:如果写了第三个参数,前两个参数返回的值将不会直接被挂到 this.props 上,所以第三个参数需要返回正确的值。

options

是否开启优化,默认值为 true。

connect 在容器组件中的用法:

function mapStateToProps(state) {
const { isFetching, objectTypes } = state.rootData.objectGroup || {
isFetching: true,
objectTypes: []
}
return { isFetching, objectTypes }
} function mapDispatchToProps(dispatch) {
const actionCreators = Object.assign({}, objectsTypeActionCreators);
return bindActionCreators(actionCreators, dispatch);
} export default connect(mapStateToProps, mapDispatchToProps)(CRMPage);

从 connect 的用法可以看出,这里 connect 接收了两个参数,分别处理 state 和 action,最终返回一个组件。最终 export 出去的不是最初定义的 React 组件,是用 connect 包裹之后的组件。所以 connect 做的事主要有:

  1. 返回一个 React 组件,将函数传入的参数(state,action)放入 this.props 下。
  2. 在组件结构加载完成后注册监听器,在组件卸载时注销监听器。监听器的作用是捕捉每一次 state 的更新。

具体实现是

  1. 在 constructor 取得当前的 store 树,可以通过 this.props.store 和 this.context.store 两种方式获取,其中第二种方式是从 Provider 的 getChildContext() 传进来的。

  2. 在 render 里使用 React 的 createElement方法生成新的组件,并将传入的 state 和 action 合并到 props 上。

render(){
this.renderedElement = createElement(WrappedComponent,
this.mergedProps //merge stateProps, dispatchProps, props
)
return this.renderedElement;
}

最后返回的时候用hoistNonReactStatic将原来组件中的元素拷贝到目标组件。

hoistNonReactStatic(targetComponent, sourceComponent);
  1. 在 componentDidMount 中使用 store 的 subscribe 方法注册监听事件,以便在 dispatch 之后调用,componentWillUnmount 执行 unsubcribe 事件,注销监听器。
trySubscribe() {
if (shouldSubscribe && !this.unsubscribe) {
this.unsubscribe = this.store.subscribe(this.handleChange.bind(this))
this.handleChange()
}
} tryUnsubscribe() {
if (this.unsubscribe) {
this.unsubscribe()
this.unsubscribe = null
}
}

这样,在每次 dispatch(action) 之后会执行 handleChange,在 handleChange 函数内部,会执行 setState 更新 store tree。

所以每次 dispatch(action) 之后,执行对应的 reducer,然后执行相应的订阅的函数。在 reducer 里把 store 更新,在监听函数里执行 setState,更新 React 组件。

react-redux 学习笔记的更多相关文章

  1. React+Redux学习笔记:React+Redux简易开发步骤

    前言 React+Redux 分为两部分: UI组件:即React组件,也叫用户自定义UI组件,用于渲染DOM 容器组件:即Redux逻辑,处理数据和业务逻辑,支持所有Redux API,参考之前的文 ...

  2. React Redux学习笔记

    React Router React Router 使用教程 Redux中间件middleware [译]深入浅出Redux中间件 Redux学习之一:何为middleware? ES6 ES6新特性 ...

  3. The Road to learn React书籍学习笔记(第二章)

    The Road to learn React书籍学习笔记(第二章) 组件的内部状态 组件的内部状态也称为局部状态,允许保存.修改和删除在组件内部的属性,使用ES6类组件可以在构造函数中初始化组件的状 ...

  4. The Road to learn React书籍学习笔记(第三章)

    The Road to learn React书籍学习笔记(第三章) 代码详情 声明周期方法 通过之前的学习,可以了解到ES6 类组件中的生命周期方法 constructor() 和 render() ...

  5. React 入门学习笔记整理目录

    React 入门学习笔记整理(一)--搭建环境 React 入门学习笔记整理(二)-- JSX简介与语法 React 入门学习笔记整理(三)-- 组件 React 入门学习笔记整理(四)-- 事件 R ...

  6. React Native 学习笔记--进阶(二)--动画

    React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...

  7. 【原】redux学习笔记

    上周学习了flux,这周研究了一下redux,其实很早之前都已经在研究他们了,只是之前一直没搞懂,最近这两周可能打通了任督二脉,都算入门了. 写博客的目的主要是做一下笔记,总结一下思路,以及和大家交流 ...

  8. The Road to learn React书籍学习笔记(第一章)

    react灵活的生态圈 Small Application Boilerplate: create-react-app Utility: JavaScript ES6 and beyond Styli ...

  9. React Native学习笔记

    React 是使用ES6 ,支持JSX语法, 开发组件化web或native的工具. 现阶段使用Babel工具转换成ES5 代码. 组件通过props属性传递不变化的内容,UI通过state属性变动来 ...

  10. redux学习笔记

    中文api:http://cn.redux.js.org/docs/react-redux/troubleshooting.html 3.6 Reducer Store 收到 Action 以后,必须 ...

随机推荐

  1. <<海闻电子发票接口 ESB 封装 代码指示 文档>>

    <<海闻电子发票接口 ESB 封装 代码指示 文档>> isValid 是否有效标志 代码 中文 说明 true 成功 false 失败   code 海闻错误说明 代码 中文 ...

  2. UVALive 7077 - Song Jiang's rank list(模拟)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  3. 题目连接:http://acm.zznu.edu.cn/problem.php?id=1329

    题目大意: 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是17的倍数时,原数也是17的倍数 . 例如,34是17的倍数,因为3-20=-17是17的倍数:20 ...

  4. codeforces 644A Parliament of Berland

    A. Parliament of Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. mysql show processlist 命令详解

    命令格式 SHOW [FULL] PROCESSLIST SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPE ...

  6. ZOJ 3195 Design the city LCA转RMQ

    题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...

  7. 一个python

    #!/usr/bin/env python #coding=utf-8 import os # 遍历文件 r=input("type a directory name:") for ...

  8. Web CORS 跨域方式使用方式

    CORS 参考 http://enable-cors.org/index.html https://help.aliyun.com/document_detail/oss/practice/cors_ ...

  9. Android学习过程中遇到的问题

    1.使用在Activity布局之上重叠显示操作栏,第一次使用出现错误信息. 错误信息如下:java.lang.RuntimeException:Ubable to start activity Com ...

  10. ENTBOOST 2014.180L 发布,开源企业IM免费企业即时通讯

    ENTBOOST,VERSION 2014.180 Linux版本发布,主要增加企业IM应用集成功能,完善安卓SDK功能及部分BUG修正: 7/1(明天)发布Windows版本,敬请关注! ENTBO ...