combineReducers
const reactInit = '@@react/Init'
const combineReducers = (reducers) => {
const finalReducers = {}
for (let key in reducers) {
const reducer = reducers[key]
if (typeof reducer === 'undefined') {
console.error(`reducer${key}的值是undefined`)
}
if (typeof reducer === 'function') {
finalReducers[key] = reducer
}
}
for (let key in finalReducers) {
const reducer = finalReducers[key]
const state = reducer(undefined, reactInit)
if (typeof state === 'undefined'){
throw new Error(`reducer${key}的返回值为undefined`)
}
}
return (state={}, action) => {
for (let key in finalReducers) {
const reducer = finalReducers[key]
const newState = reducer(state[key], action)
state[key] = newState
}
return state
}
}
combineReducers的更多相关文章
- [Redux] Reducer Composition with combineReducers()
Previous, we do composition with objects: const todoApp = (state = {}, action) => { return { todo ...
- [Redux] Implementing combineReducers() from Scratch
The combineReducers function we used in previous post: const todoApp = combineReducers({ todos, visi ...
- Redux源码分析之combineReducers
Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...
- [Redux] redux之combineReducers
combineReducers combineReducer 是将众多的 reducer 合成通过键值映射的对象,并且返回一个 combination 函数传入到 createStore 中 合并后的 ...
- redux源码学习笔记 - combineReducers
上一篇有了解到,reducer函数的两个为:当前state和此次dispatch的action. state的结构是JavaScript对象,每个key都可以代表着不同意义的数据.比如说 { list ...
- React-使用combineReducers完成对数据对拆分管理
数据都放在reducer.js下不利于对数据进行管理,可以把一个大的reducer.js拆分成多个小的reducer.js. 小的reducer.js const defaultState={ foc ...
- combineReducers 对数据进行拆分管以及使用immutable.js
1 使用combineReaducers 整合reducers import { combineReducers } from 'redux-immutable'; import { reducer ...
- 使用combineReducers注意事项
一.从‘redux’包中引入combineReducers方法: import { combineReducers } from 'redux'; 二.针对state的不同属性写不同的reducer, ...
- Redux API之combineReducers
combineReducers(reducers) 随着应用变得复杂,需要对 reducer 函数 进行拆分,拆分后的每一块独立负责管理 state 的一部分. combineReducers 辅助函 ...
随机推荐
- python摸爬滚打之----tcp协议的三次握手四次挥手
TCP协议的三次握手, 四次挥手 三次握手过程 1, 服务器时刻准备接受客户端进程的连接请求, 此时服务器就进入了LISTEN(监听)状态; 2, 客户端进程然后向服务器发出连接请求报文, 之后客户端 ...
- Java中equals,hashcode,==的区别
== :比较java栈局部变量表中变量的地址或值是否相等. equals : 比较变量的地址在java堆中引用对象是否为同一个对象. hashcode : 通过对象在JVM内存中的存储地址通过特定算 ...
- Redis Sentinel实现的机制与原理详解
序言 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案.实际上这意味着你可以使用Sentinel模式创建一个可以不用人为干预而应对各种故障的Redis部署. 它的主要功能有以 ...
- javascript的ES6学习总结(第一部分)
ES6(ESNext学习总结——第一部分) ES6, 全称 ECMAScript 6.0 ,是 JavaScript 的下一个版本标准,2015.06 发版. ECMA每年6月份,发布一个版本 201 ...
- 接口测试工具-poster
1.发包工具-火狐浏览器插件 安装:打开火狐浏览器-alt键-工具-附加组件-搜索poster-点击安装-重启火狐浏览器-打开工具下的poster 2.使用 1)GET 1.输入URL 2.填入参数 ...
- Json字符串转map集合
第一步:在pom.xml中添加依赖; <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...
- Matlab文件和数据的导入与导出
ref: https://blog.csdn.net/zengzeyu/article/details/72530596 Matlab文件和数据的导入与导出 2017年05月19日 15:18:35 ...
- Go 初体验 - 并发与锁.2 - sync.WaitGroup
sync包里的WaitGroup主要用于协程同步 计数主协程创建的子线程 WaitGoup.Add(i) 调用清除标记方法WaitGroup.Done() 使用WaitGroup.Wait()来阻塞, ...
- 【JavaScript】学习中遇到的一些问题
一.JavaScript中没法直接比较两个object和array是否相等
- 关于Win10安装vs2013简体中文语言包无法安装的问题
Win10下安装VS2013后无法安装VS2013中文简体语言包,提示Windows程序兼容模式已打开. 解决方案: 1,随便找个文件夹将简体中文语言包放入其中,发送快捷方式到桌面,右击快捷方式 选择 ...