自定义React-redux
实现mini版react-redux
1. 理解react-redux模块
1). react-redux模块整体是一个对象模块
2). 包含2个重要属性: Provider和connect
3). Provider
值: 组件类
作用: 向所有容器子组件提供全局store对象
使用: <Provider store={store}><Xxx/></Provider>
4). connect
值: 高阶函数
作用: 包装组件生成容器组件, 让被包装组件能与redux进行通信
使用: connect(mapStateToProps, mapDispatchToProps)(Xxx)
2. context的理解和使用
1). 理解
当你觉得多层传递props麻烦, 可以选择使用context
context是组件对象的一个属性, 它的值是一个对象
一个组件指定的context内数据, 所有层次子组件都可以读取到
如果可以尽量不用context, 你可以选择使用react-redux, react-redux内部就利用了context
2). 使用
父组件:
static childContextTypes = {
color: PropTypes.string
}
getChildContext() {
return {color: 'red'};
}
后代组件:
static contextTypes = {
color: PropTypes.string
}
render () {
this.context.color
}
3. 实现代码: src/libs/react-redux/index.js
import React, {Component} from 'react'
import PropTypes from 'prop-types'
/*
1. Provider组件类
*/
export class Provider extends Component {
// 声明当前组件接收store
static propTypes = {
store: PropTypes.object.isRequired
}
// 必须声明向子节点指定全局数据store
static childContextTypes = {
store: PropTypes.object.isRequired
}
// 指定向子组件指定全局数据store
getChildContext() {
return {store: this.props.store};
}
render() {
// 返回所有子节点(如果没有子节点返回undefined, 如果只有一个子节点它是对象, 如果有多个它是数组)
return this.props.children
}
}
/*
2. connect方法
*/
export function connect(mapStateToProps = () => null, mapDispatchToProps = {}) {
// 返回函数(接收被包装组件类作为参数)
return (WrapComponent) => {
// 返回一个新的组件类
return class ConnectComponent extends Component {
// 声明接收全局store
static contextTypes = {
store: PropTypes.object.isRequired
}
// 构造函数的第2个参数为context对象
constructor(props, context) {
super(props)
console.log('constructor', this.context) // 此时组件对象中还没有context
// 从context中取出store
const {store} = context
// 一般属性: 调用mapStateToProps函数得到包含所有需要传递一般属性的集合对象
const stateProps = mapStateToProps(store.getState())
// 分发action的函数属性: 调用自定义的整合函数生成包含多个分发action的函数的对象
const dispatchProps = this.bindActionCreators(mapDispatchToProps)
// 初始化状态, 包含所有需要传递给WrapComponent组件的一般属性
this.state = {
...stateProps
}
// 将包含dispatch函数的对象保存在组件对象上(不用放到state中)
this.dispatchProps = dispatchProps
}
/*
根据包含多个action creator的对象, 返回一个包含多个分发action的函数的对象
*/
bindActionCreators = (mapDispatchToProps) => {
// 准备一个保存分发action函数的对象容器
const dispatchProps = {}
// 遍历每个action creator
Object.keys(mapDispatchToProps).forEach((key) => {
// 得到某个action creator
const actionCreator = mapDispatchToProps[key]
//定义包含分发action代码的函数, 并只在到准备好的容器中
dispatchProps[key] = (...args) => {
this.context.store.dispatch(actionCreator(...args))
}
})
// 返回dispatch代码函数容器对象
return dispatchProps
}
componentDidMount() {
console.log('componentDidMount', this.constructor)
// 得到store
const {store} = this.context
// 订阅监听
store.subscribe(() => {
// 一旦store中的state有变化, 更新组件状态, 从而导致被包装组件重新渲染
this.setState(mapStateToProps(store.getState()))
})
}
render() {
return <WrapComponent {...this.state} {...this.dispatchProps} />
}
}
}
}
自定义React-redux的更多相关文章
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- webpack+react+redux+es6
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例
工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...
- react + redux 完整的项目,同时写一下个人感悟
先附上项目源码地址和原文章地址:https://github.com/bailicangd... 做React需要会什么? react的功能其实很单一,主要负责渲染的功能,现有的框架,比如angula ...
- 看了就学会之React redux入门示例
环境准备 为了方便,这里使用create-react-app搭建react环境 create-react-app mydemo 弹出配置 如果需要自定义react的配置,需要运行下面的命令把配置文件弹 ...
- react+redux教程(六)redux服务端渲染流程
今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...
- react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...
- react+redux官方实例TODO从最简单的入门(6)-- 完结
通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...
- react+redux官方实例TODO从最简单的入门(1)-- 前言
刚进公司的时候,一点react不会,有一个需求要改,重构页面!!!完全懵逼,一点不知道怎么办!然后就去官方文档,花了一周时间,就纯react实现了页面重构,总体来说,react还是比较简单的,由于当初 ...
- 重写官方TodoList,对于初学react+redux的人来说,很有好处
虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...
随机推荐
- mac 软件相关的
mac 系统教学 https://www.w3cschool.cn/macdevsetup/carp1i83.html 可以查看的软件网站 https://www.ifunmac.com/ https ...
- Linux安装Redis,在测试阶段即make test出现“You need tcl 8.5 or newer in order to run the Redis test”问题解决方案
Linux安装Redis,在测试阶段即make test出现"You need tcl 8.5 or newer in order to run the Redis test"问题 ...
- 量化投资学习笔记27——《Python机器学习应用》课程笔记01
北京理工大学在线课程: http://www.icourse163.org/course/BIT-1001872001 机器学习分类 监督学习 无监督学习 半监督学习 强化学习 深度学习 Scikit ...
- js能力测评——查找元素的位置
查找元素的位置 题目描述: 找出元素 item 在给定数组 arr 中的位置 输出描述: 如果数组中存在 item,则返回元素在数组中的位置,否则返回 -1 示例1 输入 [ 1, 2, 3, 4 ] ...
- springboot之swagger快速启动(新的ui)
springboot之swagger快速启动(新的ui) 功能点: 集成swagger前端接口文档 Swagger 整合 zuul 智能列表 无缝集成 knife4j 前端文档 支持 v0.1.2RE ...
- Redis-位图
关于位图,可能大家不太熟悉, 那么位图能干啥呢?位图的内容其实就是普通的字符串,也就是byte数组,我们都知道 byte 8 位无符号整数 0 到 255 说个场景.比如你处理一些业务时候,往往会存在 ...
- Linux系统开机显示BusyBox v1.22.1 built-in shell(ash) 解决方法
BusyBox 是一个集成了三百多个最常用Linux命令和工具的软件.BusyBox 包含了一些简单的工具,例如ls.cat和echo等等,还包含了一些更大.更复杂的工具,例grep.find.mou ...
- JS中变量、作用域的本质,定义及使用方法
全局作用域和局部作用域 全局作用域 局部作用域:函数作用域 全局作用域在全局和局部都可以访问到,局部作用域只能在局部被访问到 var name="cyy"; function fn ...
- 出现ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly ....的解决方法
在terminal上运行gluoncv时遇到了一个报错问题. ImportError: Python is not installed as a framework. The Mac OS X bac ...
- vue-cli-service 报错
错误内容: vue-cli-service serve /bin/sh: vue-cli-service: command not found error Command failed with ex ...