[Redux] Reducer Composition with Arrays
In the previous lesson we created a reducer that can handle two actions, adding a new to-do, and toggling an existing to-do. Right now, the code to update the to-do item or to create a new one is placed right inside of the to-dos reducer.
This function is hard to understand because it makes us two different concerns, how the to-do's array is updated, and how individual to-dos are updated. This is not a problem unique to Redux. Any time a function does too many things, you want to extract other functions from it, and call them so that every function only addresses a single concern.
In this case, I decided that creating and updating a to-do in response to an action is a separate operation, and needs to be handled by a separate function called to-do. As a matter of convention, I decided that it should also accept two arguments, the current trait and the action being dispatched, and it should return the next trait.
But in this case, this trait refers to the individual to-do, and not to the least of to-dos. Finally, there is no magic in Redux to make it work. We extracted the to-do reducer from the to-dos reducer, so now we need to call it for every to-do, and assemble the results into an array.
While this is not required in this particular example, I suggest that you always have the default case where you return the current trait to avoid all [inaudible 1:36] in the future. The part described in this lesson is pervasive in Redux's development, and is called reducer composition.
Different reducers specify how different parts of the trait tree are updated in response to actions. Reducers are also normal JavaScript functions, so they can call other reducers to delegate and abstract a way of handling of updates of some parts of this tree they manage.
This pattern can be applied many times, and while there is still a single top level reducer managing the state of your app, you will find it convenient to express it as many reducers call on each other, each contribution to a part of the applications trait tree.
let todo = (state, action) => {
switch(action.type){
case 'ADD_ITEM':
return {
text: action.text,
id: action.id,
completed: false
};
case 'TOGGLE_ITEM':
if(state.id !== action.id){
return state;
}else{
return {
...state,
completed: !state.completed // will overwirte the state object's completed prop
};
}
default:
return state;
}
} let todos = (state = [], action) => { switch(action.type){
case 'ADD_ITEM':
return state = [
...state,
todo(undefined, action)
];
case 'TOGGLE_ITEM':
return state.map( (t) => todo(t, action))
default:
return state;
}
}; let testTodo_addItem = () => {
let stateBefore = [];
let action = {
type: 'ADD_ITEM',
text: 'Learn Redux',
id: 0
};
let stateAfter = [
{
text: 'Learn Redux',
id: 0,
completed: false,
}
]; deepFreeze(stateBefore);
deepFreeze(action); expect(
todos(stateBefore, action)
).toEqual(stateAfter);
}; let testTodo_toggleItem = () => {
let stateBefore = [
{
text: 'Learn Redux',
id: 0,
completed: false
},
{
text: 'Learn Angular2',
id: 1,
completed: false
}
];
let action = {
type: 'TOGGLE_ITEM',
id: 1
}; let stateAfter = [
{
text: 'Learn Redux',
id: 0,
completed: false
},
{
text: 'Learn Angular2',
id: 1,
completed: true
}
]; deepFreeze(stateBefore);
deepFreeze(action); expect(
todos(stateBefore, action)
).toEqual(stateAfter);
} testTodo_toggleItem(); console.log("All tests passed!");
[Redux] Reducer Composition with Arrays的更多相关文章
- [Redux] Reducer Composition with combineReducers()
Previous, we do composition with objects: const todoApp = (state = {}, action) => { return { todo ...
- redux reducer笔记
踩坑一,reducer过于抽象 reducer写得没那么抽象也不会有人怪你的.^_^ reducer其实只有一个,由不同的reducer composition出来的.所以, reducer的父作用域 ...
- Redux生态系统
生态系统 Redux 是一个体小精悍的库,但它相关的内容和 API 都是精挑细选的,足以衍生出丰富的工具集和可扩展的生态系统. 如果需要关于 Redux 所有内容的列表,推荐移步至 Awesome R ...
- react+redux教程(四)undo、devtools、router
上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...
- [React Testing] Redux Reducers
Sometimes we want to test our Redux reducers to make sure they work as expected. In this lesson we w ...
- 深入Redux架构
关于redux 之前写了一篇通过一个demo了解Redux,但对于redux的核心方法没有进行深入剖析,在此重新总结学习,完整的代码看这里.(参考了React 技术栈系列教程) 什么情况需要用redu ...
- redux入门指南
前言:大概一个月没有写博客了,这两天正好是周末,就写点东西来梳理下之前几个月的所写与所得; 大概两个月前,学习了一下 redux ,还是一点难度的,花了我一天的时间来搞明白他, 但是都没怎么记录,今天 ...
- redux (一)
redux 是一个状态管理的库. redux认为页面所有的变化,都是基于状态的改变触发的,所以我们维护一个应用的时候,都是在维护这些状态.而 redux 就是为了维护状态而生的. API create ...
- React从入门到放弃之前奏(3):Redux简介
安装 npm i -S redux react-redux redux-devtools 概念 在redux中分为3个对象:Action.Reducer.Store Action 对行为(如用户行为) ...
随机推荐
- iOS开发 UITableView之cell
1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...
- 10个加速Table Views开发的Tips(转)
本文由CocoaChina译者yake_099(博客)翻译,作者:David McGraw原文:10 Actionable Performance Tips To Speed Up Your Tabl ...
- JavaScript--模拟验证码
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- MYSQL :逗号分隔串表,分解成竖表
DROP TEMPORARY TABLE IF EXISTS Temp_Num ; CREATE TEMPORARY TABLE Temp_Num ( xh INT PRIMARY KEY ); -- ...
- ASP.NET MVC中的模型绑定
模型绑定的本质 任何控制器方法的执行都受action invoker组件(下文用invoker代替)控制.对于每个Action方法的参数,这个invoker组件都会获取一个Model Binder ...
- Mysql 源码编译教程贴
题外话:这是一篇教程贴,不仅学的是mysql的编译,还是一些编译的知识.我也是一个菜鸟,写一些感悟和心得,有什么问题可以批评指正,谢谢! 如果只是为了安装请移到我的另一篇安装贴: Mysql安装贴 环 ...
- 66 Plus One(大数+1Easy)
题目意思:vector<int> v存数 eg.123 则v[0]=1,v[1]=2,v[2]=3,加1后返回一个vector 思路:先判断新的vector长度是否需要加1,然后从v ...
- C# ORM—Entity Framework 之Database first(数据库优先)&Model First(模型优先)(一)
一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...
- JS之路——浏览器window对象
window对象的方法 window.alert(msg) window.close() window.print() var a = window.setIntval(function,毫秒) // ...
- 转:PHP 使用ZipArchive压缩文件并下载
原文来自于:http://courages.us/archives/176 网站上需要提供一些打印数据给用户下载,这些文件每次都需要重新生成,因为随时都会有新的数据产生.网络上关于PHP的压缩功能实现 ...