react-redux中的数据传递
1、connect
connect用于连接React组件与 Redux store,其使用方法如下
connect([mapStateToProps], [mapDispatchToProps], [mergeProps],[options])
[mapStateToProps(state, [ownProps]): stateProps]是connect的第一个参数,其类型为function,允许我们将 store 中的数据作为 props 绑定到组件上。
const mapStateToProps = (store) => {
return {
count:store.count
}
}
(1)这个函数的第一个参数就是 Redux 的 store,我们不必将 store中的数据原封不动地传入组件,可以根据 state 中的数据,动态地输出组件需要的(最小)属性。
(2)函数的第二个参数 ownProps,是组件自己的 props。有的时候,ownProps 也会对其产生影响。
当 state 变化,或者 ownProps 变化的时候,mapStateToProps 都会被调用,计算出一个新的 stateProps,(在与 ownProps merge 后)更新给组件。
[mapDispatchToProps(dispatch, ownProps): dispatchProps]将 action 作为 props 绑定到组件上,也会成为 MyComp 的 props。
stateProps 和 dispatchProps,都需要和 ownProps merge 之后才会被赋给组件。connect 的第三个参数就是用来做这件事。如果不传这个参数,connect 就会使用 Object.assign替代该方法。
connect 的第四个参数[options] (Object) 如果指定这个参数,可以定制 connector 的行为,一般不用。
connect核心代码:
export default function connect(mapStateToProps, mapDispatchToProps, mergeProps, options = {}) {
return function wrapWithConnect(WrappedComponent) {
class Connect extends Component {
constructor(props, context) {
// 从祖先Component处获得store
this.store = props.store || context.store
this.stateProps = computeStateProps(this.store, props)
this.dispatchProps = computeDispatchProps(this.store, props)
this.state = { storeState: null }
// 对stateProps、dispatchProps、parentProps进行合并
this.updateState()
}
shouldComponentUpdate(nextProps, nextState) {
// 进行判断,当数据发生改变时,Component重新渲染
if (propsChanged || mapStateProducedChange || dispatchPropsChanged) {
this.updateState(nextProps)
return true
}
}
componentDidMount() {
// 改变Component的state
this.store.subscribe(() = {
this.setState({
storeState: this.store.getState()
})
})
}
render() {
// 生成包裹组件Connect
return (
<WrappedComponent {...this.nextState} />
)
}
}
Connect.contextTypes = {
store: storeShape
}
return Connect;
}
}
可以看到connect是一个高阶函数
首先,传入mapStateToProps、mapDispatchToProps
然后,返回一个生产Component的函数(wrapWithConnect)
最后,将真正的Component作为参数传入wrapWithConnect
这样就生产出一个经过包裹的Connect组件,该组件具有如下特点:
通过props.store获取祖先Component的storeprops包括stateProps、dispatchProps、parentProps,合并在一起得到nextState,作为props传给真正的Component
componentDidMount时,添加事件this.store.subscribe(this.handleChange),实现页面交互
shouldComponentUpdate时判断是否有避免进行渲染,提升页面性能,并得到nextState
componentWillUnmount时移除注册的事件this.handleChange
2、Provider
Provider组件主要有以下两个作用:
1、在原应用组件上包裹一层,使原来整个应用成为Provider的子组件
2、接收Redux的store作为props,通过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)
}
}
if (process.env.NODE_ENV !== 'production') {
Provider.prototype.componentWillReceiveProps = function (nextProps) {
const { store } = this
const { store: nextStore } = nextProps
if (store !== nextStore) {
warnAboutReceivingStore()
}
}
}
Provider.propTypes = {
store: storeShape.isRequired,
children: PropTypes.element.isRequired
}
Provider.childContextTypes = {
store: storeShape.isRequired
}
从上面的代码可以看出Provider是通过context传递给子组件的,子组件通过connect获得数据,实现过程如下,可以看到在没有定义props的情况下,通过context直接取得store中的数据。
...
constructor(props, context) {
this.store = props.store || context.store
this.stateProps = computeStateProps(this.store, props)
this.dispatchProps = computeDispatchProps(this.store, props)
this.state = { storeState: null }
this.updateState()
}
...
转自:https://www.jianshu.com/p/5726bb042bda
react-redux中的数据传递的更多相关文章
- 在react/redux中使用Immutable
在redux中使用Immutable 1.什么是Immutable? Immutable是一旦创建,就不能被更改的数据. 对Immutable对象的任何修改或添加删除操作都会返回一个新的Immutab ...
- web开发-前端到服务器Controller中的数据传递
一, ajax方式 1. ajax获取页面中的数据,包括表单中的数据, 然后封装成对象,数组, 字符串, 或其他基本类型的数据. 2. 将封装得到的数据通过ajax传递到controller中(注:在 ...
- OpenGL进阶(十一) - GLSL4.x中的数据传递
in out 对于 vertex shader,每个顶点都会包含一次,它的主要工作时处理关于定点的数据,然后把结果传递到管线的下个阶段. 以前版本的GLSL,数据会通过一些内建变量,比如gl_Vert ...
- 将Controller中的数据传递到View中显示
如何将Controller 中的数据传送到View 步骤: (1)要有数据,如果要用到对象可以在Model 中定义对应的类 (2)要有装数据的容器: System.Text.StringBuilder ...
- 后端list集合中的数据传递到前台HTML中显示(表格形式)
关键字:web项目中前后台数据传递问题 在学习web项目的过程中,我们肯定会遇到前后台数据交换问题.这个问题我也思考了很久,今天借此总结一下.由于博主水平有限,如有不当之处,还请大家多多指正,,废话不 ...
- 剖析 Rails 3 MVC 中的数据传递
引用链接:https://www.ibm.com/developerworks/cn/web/1108_linhx_rails3mvc/ 如果读者已经开发过基于 Rails 的应用,但对其 MVC 间 ...
- Android笔记——Activity中的数据传递案例(用户注冊)
1.创建程序activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- web开发-服务器Controller到前端中的数据传递
一, ajax方式 (一)controller中 1. 定义AjaxResponse类 成员有: status , message, data. 其中 status是成功或失败状态, message ...
- springmvc中的数据传递
import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; impo ...
随机推荐
- JVM探秘6--图解虚拟机栈的局部变量表和操作数栈工作流程
案例代码如下: public class JVMTest { public static Integer num = 10; public int add(int i){ int j = 5; int ...
- redis(一主两从三哨兵模式搭建)记录
转自:http://www.cnblogs.com/fly-piglet/p/9836314.html 目的: 让看看这篇文章的的人能够知道:软件架构.软件的安装.配置.基本运维的操作.高可用测试.也 ...
- scrapy的splash 的简单使用
安装Splash(拉取镜像下来)docker pull scrapinghub/splash安装scrapy-splashpip install scrapy-splash启动容器docker run ...
- 原生js移除或添加样式
样式效果如下,点击商品详情 添加样式active 代码 <!doctype html> <html lang="en"> <head> < ...
- 解决 httpclient 下 Address already in use: connect 的错误
最近做httpclient做转发服务,发现服务器上总是有很多close_wait状态的连接,而且这些连接都不会关闭,最后导致服务器没法建立新的网络连接,从而停止响应. 后来在网上搜索了一下,发现解决的 ...
- Bugku-CTF之备份是个好习惯
Day17 备份是个好习惯 听说备份是个好习惯 http://123.206.87.240:8002/web16/
- MSSQL 漏洞利用与提权
1.SA口令的获取 webshell或源代码的获取 源代码泄露 嗅探(用CAIN等工具嗅探1433数据库端口) 口令暴力破解 2.常见SQL server 提权命令 查看数据库的版本(select @ ...
- C#线程同步(5)- 信号量 Semaphore
文章原始出处 http://xxinside.blogbus.com/logs/47617134.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...
- eclipse中出现An internal error occurred during: "Initializing Java Tooling"
关于这个问题我查了一下,就是删除.projct文件夹下的文件. 自己试了一下,这个可以及解决问题可是会出现新的问题. 1.SVN关联没了,这样做你的svn信息都没了,项目还要重新导一遍 2.出现了新的 ...
- Python3 tkinter基础 Tk quit 点击按钮退出窗体
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...