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. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...
随机推荐
- PHP 去一定范围随机小数 随机浮点数
例如取2到3中的 随机小数(一位)或整数 mt_rand(20,30)/10 mt_rand()是随机取整函数 先扩大一定倍数,再缩小相应倍数,倍数代表精确到哪一位
- cookielib模块 for python3
python2 可以直接安装cookielib模块 而py3却不能安装 故需要安装http模块 举例子: from http import cookiejar cookie = cookiejar.C ...
- MNIST机器学习入门(二)
在前一个博客中,我们已经对MNIST 数据集和TensorFlow 中MNIST 数据集的载入有了基本的了解.本节将真正以TensorFlow 为工具,写一个手写体数字识别程序,使用的机器学习方法是S ...
- [cf 1194 D] 1-2-K Game
(当时让这道sb题卡住了,我比sb还sb) 题意: n个东西,两个人轮流取,每次可以取走1个,2个或k个,不能取的人输,求谁必胜. $0\leq n \leq 10^{9},3\leq k \leq ...
- win7安装镜像注入USB3.0,NVMe驱动
现在的新款主板和笔记本因为原生自带了USB3.0和NVMe,在安装WIN7的时候会出现进入安装界面后不识别USB设备且在硬盘列表中无法读取M.2类型的固态硬盘信息.导致这个现象的原因就是在WIN7安装 ...
- EF6 + MySql 建立项目引用失败
EF6 + MySql 建立项目 步骤 在项目中使用” NuGet” 包添加 EntityFramework 和 MySql.Data ,如下图 (1) 在NuGet界面中的“浏览”选项卡 ...
- 2.NET Core设定数据库种子
1.使用以下代码在 Models 文件夹中创建一个名为 SeedData 的新类: using Microsoft.EntityFrameworkCore;using Microsoft.Extens ...
- mybatis使用foreach处理List中的Map mybatis-----传入传出多个参数,都是map或list,批量更新
https://nannan408.iteye.com/blog/2170470 https://blog.csdn.net/xingzhishen/article/details/86424395 ...
- PHP实现curl post和get
CURL这里就不说明了.以下是简单案例 一.POST //初始化 $curl = curl_init(); //设置抓取的url curl_setopt($curl, CURLOPT_URL, 'ht ...
- How to set up "lldb_codesign" certificate!
To use the in-tree debug server on macOS, lldb needs to be code signed. TheDebug, DebugClang and Rel ...