React-Redux常见API
React-Redux是在Redux的基础上,将其大量重复的代码进行了封装。
1. Provider
利用context对象给所有子组件提供store。不再需要在每个组件都引用store。
import React, { Component } from 'react';
import Context from './context';
// 给connect等方法提供store
export default class Provider extends Component {
render() {
return (
<Context.Provider value={{store: this.props.store}}>
{this.props.children}
</Context.Provider>
)
}
}
2. connect
该方法封装了大量的逻辑,主要如下:
1. 给使用connect方法的组件属性自动绑定了dispatch方法;this.props.dispatch
2. 给使用connect方法的组件的setState方法自动添加了对仓库的state的订阅
3. 给使用connect方法的组件的属性绑定仓库的state值;this.props.XXXX
不再使用store.getState方法
4. 给使用connect方法的组件的actions自动使用bindActionCreators方法
import React, { Component } from 'react';
import Context from './context';
import { bindActionCreators } from 'redux';
/**
*
* @param {function} mapStateToProps 绑定state到组件的props
* @param {funtion|object} mapDispatchToProps 返回actions对象
*/
export default function(mapStateToProps, mapDispatchToProps) {
return function(WrappedComponent) {
return class extends Component {
static contextType = Context;
constructor(props, context) {
super(props);
// 被映射的state, 即mapStateToProps的返回值, 绑定到组件的props上
this.state = mapStateToProps(context.store.getState());
}
componentDidMount() {
this.unsubscribe = this.context.store.subscribe(() => {
// setState的用法;传一个state对象
this.setState(mapStateToProps(this.context.store.getState()));
})
}
componentWillUnmount() {
this.unsubscribe();
}
render() {
const { dispatch } = this.context.store;
let actions = {};
if (typeof mapDispatchToProps === 'object'){
actions = mapDispatchToProps;
}
if (typeof mapDispatchToProps === 'function') {
actions = mapDispatchToProps(dispatch);
}
const bindActions = bindActionCreators(actions, dispatch)
return (
<WrappedComponent dispatch={dispatch} {...this.state} {...bindActions} />
)
}
}
}
}
React-Redux常见API的更多相关文章
- react第十六单元(redux的认识,redux相关api的掌握)
第十六单元(redux的认识,redux相关api的掌握) #课程目标 掌握组件化框架实现组件之间传参的几种方式,并了解两个没有任何关系组件之间通信的通点 了解为了解决上述通点诞生的flux架构 了解 ...
- react+redux渲染性能优化原理
大家都知道,react的一个痛点就是非父子关系的组件之间的通信,其官方文档对此也并不避讳: For communication between two components that don't ha ...
- ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例
工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...
- Immutable.js 以及在 react+redux 项目中的实践
来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- react+redux教程(六)redux服务端渲染流程
今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...
- react+redux官方实例TODO从最简单的入门(6)-- 完结
通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...
- react+redux教程(四)undo、devtools、router
上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...
- react+redux教程(三)reduce()、filter()、map()、some()、every()、...展开属性
reduce().filter().map().some().every()....展开属性 这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...
- react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware
今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...
随机推荐
- Window中C++进行精确计时的方法
嗯,程序员一个永恒的追求就是性能吧? 为了衡量性能,自然需要计时. 奈何无论C标准库还是C++标准库,因为通用性的考虑,其time API精度都不高.基本都是毫秒级的. 所以如果要真正精确地衡量程序的 ...
- java Unicode和UTF-8之间转换
utf-8转unicode public static String utf8ToUnicode(String inStr) { char[] myBuffer = inStr.toCharArray ...
- C#基础—数组
C#基础之数组 1. 数组的定义与初始化 一维数组: (1) int [] A = new int[4]{ 0,1,2,3}; (2) int[] B ...
- 2019 魔域java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.魔域等公司offer,岗位是Java后端开发,因为发展原因最终选择去了魔域,入职一年时间了,也成为了面试官,之 ...
- 证券secuerity经济术语
证券按其性质不同,证券可以分为证据证券.凭证证券和有价证券三大类.证据证券只是单纯地证明一种事实的书面证明文件,如信用证.证据.提单等:凭证证券是指认定持证人是某种私权的合法权利者和持证人纪行的义务有 ...
- 折叠面板实现,上传文件进度条,三级联选择器,多级联选择器, 利用layui实现
首先贴出html代码 <form class="layui-form" action=""> <div class="layui-f ...
- 【Tomcat】虚拟主机
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...
- RocketMQ-Console安装
1.获取源码 git clone -b release-rocketmq-console- https://github.com/apache/rocketmq-externals.git 2.进入工 ...
- 查看zookeeper版本
命令 echo stat|nc localhost 2181 zookeeper@kafka-zookeeper-0:/$ echo stat|nc localhost 2181 Zookeeper ...
- Gtest:事件
前言 有时候在测试的时候,我们会在测试前做一些初始化活动,和测试后做一些清理工作,gtest提供了多种事件机制,非常方便我们在案例之前或之后做一些操作.总结一下gtest的事件一共有3种: 全局的,所 ...