[Redux] Composition with Objects
For example, current we have those todos:
{
todos: [ {
completed: true,
id: 0,
text: "Learn Redux"
}, {
completed: false,
id: 1,
text: "Go shopping"
}],
}
And now what we want to do is add a 'visibilityFilter' prop to the object, composition with object enables us easily create a new reducer that calls the existing reducers to manage parts of its state and combines the results in a single state object and we don't need to change our current reducer.
Our current reducer:
const todo = (state, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
completed: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
} return {
...state,
completed: !state.completed
};
default:
return state;
}
}; const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [
...state,
todo(undefined, action)
];
case 'TOGGLE_TODO':
return state.map(t => todo(t, action));
default:
return state;
}
}; const todoApp = (state = {}, action) => {
return {
todos: todos(
state.todos,
action
)
};
};
Add a new reducer:
const todo = (state, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
completed: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
} return {
...state,
completed: !state.completed
};
default:
return state;
}
}; const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [
...state,
todo(undefined, action)
];
case 'TOGGLE_TODO':
return state.map(t => todo(t, action));
default:
return state;
}
}; const visibilityFilter = (
state="SHOW_ALL",
action
) => {
switch(action.type){
case 'SET_VISIBILITY_FILTER':
return action.filter;
break;
default:
return state;
}
} const todoApp = (state = {}, action) => {
return {
todos: todos(
state.todos,
action
),
visibilityFilter: visibilityFilter(
state.visibilityFilter,
action
)
};
};
-------------------
const todo = (state, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
completed: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
} return {
...state,
completed: !state.completed
};
default:
return state;
}
}; const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [
...state,
todo(undefined, action)
];
case 'TOGGLE_TODO':
return state.map(t => todo(t, action));
default:
return state;
}
}; const visibilityFilter = (
state = 'SHOW_ALL',
action
) => {
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
default:
return state;
}
}; const todoApp = (state = {}, action) => {
return {
todos: todos(
state.todos,
action
),
visibilityFilter: visibilityFilter(
state.visibilityFilter,
action
)
};
}; const { createStore } = Redux;
const store = createStore(todoApp); console.log('Initial state:');
console.log(store.getState());
console.log('--------------'); console.log('Dispatching ADD_TODO.');
store.dispatch({
type: 'ADD_TODO',
id: 0,
text: 'Learn Redux'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------'); console.log('Dispatching ADD_TODO.');
store.dispatch({
type: 'ADD_TODO',
id: 1,
text: 'Go shopping'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------'); console.log('Dispatching TOGGLE_TODO.');
store.dispatch({
type: 'TOGGLE_TODO',
id: 0
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------'); console.log('Dispatching SET_VISIBILITY_FILTER');
store.dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: 'SHOW_COMPLETED'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
"Initial state:"
[object Object] {
todos: [],
visibilityFilter: "SHOW_ALL"
}
"--------------"
"Dispatching ADD_TODO."
"Current state:"
[object Object] {
todos: [[object Object] {
completed: false,
id: 0,
text: "Learn Redux"
}],
visibilityFilter: "SHOW_ALL"
}
"--------------"
"Dispatching ADD_TODO."
"Current state:"
[object Object] {
todos: [[object Object] {
completed: false,
id: 0,
text: "Learn Redux"
}, [object Object] {
completed: false,
id: 1,
text: "Go shopping"
}],
visibilityFilter: "SHOW_ALL"
}
"--------------"
"Dispatching TOGGLE_TODO."
"Current state:"
[object Object] {
todos: [[object Object] {
completed: true,
id: 0,
text: "Learn Redux"
}, [object Object] {
completed: false,
id: 1,
text: "Go shopping"
}],
visibilityFilter: "SHOW_ALL"
}
"--------------"
"Dispatching SET_VISIBILITY_FILTER"
"Current state:"
[object Object] {
todos: [[object Object] {
completed: true,
id: 0,
text: "Learn Redux"
}, [object Object] {
completed: false,
id: 1,
text: "Go shopping"
}],
visibilityFilter: "SHOW_COMPLETED"
}
"--------------"
[Redux] Composition with Objects的更多相关文章
- [Redux] Reducer Composition with combineReducers()
Previous, we do composition with objects: const todoApp = (state = {}, action) => { return { todo ...
- 关联,聚合和组合(复合)--Association, Aggregation and Composition
概要 Association, Aggregation and Composition are terms that represent relationships among objects. Th ...
- 【OOAD】面向对象设计原则概述
软件的可维护性和可复用性 知名软件大师Robert C.Martin认为一个可维护性(Maintainability) 较低的软件设计,通常由于如下4个原因造成: 过于僵硬(Rigidity) ...
- Java Programming Guidelines
This appendix contains suggestions to help guide you in performing low-level program design and in w ...
- DVB-subtitle解析流程浅
DTV包含SUBTITLE和TTX. PMT中分别有不同的描述符对应,如下图的TTX descripter=0x56.语言ISO-639="fin" subtitle descri ...
- Object Pascal中文手册 经典教程
Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...
- [Redux] Reducer Composition with Arrays
In the previous lesson we created a reducer that can handle two actions, adding a new to-do, and tog ...
- Redux源码学习笔记
https://github.com/reduxjs/redux 版本 4.0.0 先了解一下redux是怎么用的,此处摘抄自阮一峰老师的<Redux 入门教程> // Web 应用是一个 ...
- redux源码解析-函数式编程
提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript> ...
随机推荐
- Cacti添加threshold、monitor和setting
Cacti版本:Version 0.8.8b 一.插件介绍: monitor:通过简单明了的图标提供服务器的运行状态 settings:给不同的插件提供一些共用的信息,如邮件信息,dns信息thold ...
- 在SpringMVC框架下建立Web项目时web.xml到底该写些什么呢?
刚刚初学Spring MVC,却连一个简单的helloworld都搞的懵懵懂懂的,配置文件搞不清,各种文件之间的逻辑关系也不懂,连续看了好些日子的Spring MVC了,今天终于下定决心,每天记录一点 ...
- jquery cookies(2)用法实现
example $.cookie('name', ‘value'); 设置cookie的值,把name变量的值设为value example $.cookie('name', ‘value', {ex ...
- asp.net webform 局部发布更新
一:关于webform编译 编译时会将每个aspx文件单独生成dll文件于bin目录下.也会将引用的dll存放于bin目录 二:对界面或者引用的dll(如BLL层,DAL层等)做了修改更新后在服务器只 ...
- ES聚合实例
在es中需要根据app_categor进行聚合,JSON查询语句如下: { "query": { "bool": { "must": [ { ...
- TIOBE.2017.01最新编程语言排行榜
Jan 2017 Jan 2016 Change Programming Language Ratings Change1 1 Java ...
- asp.net(C#)禁止缓存文件
IIS会按文件地址及参数将文件缓存到客户端,以便再次访问该内容时速度更快.如果要取消这种机制则需要禁止缓存文件. 一.编程方式 Response.Buffer = true; Response.Exp ...
- 转:Hprose for php(一)——快速入门
文章来自于:http://blog.csdn.net/half1/article/details/21095665 本文参考了Hprose官方的用户手册,Hprose官网:http://www.hpr ...
- c语言用封装来优化程序
一.基础研究 先对函数fa进行研究,代码如下: fa函数的参数为一个字符指针,他存储要输出的字符串.因为要显示在屏幕的中央位置,所以我们要把字符串放在段地址b800处.用strlen获取字符串的长度, ...
- [Android] hid设备按键流程简述
hexdump /dev/hidraw0就能看到usbhid设备传输过来的裸流 如:按下Input键 003ae60 0000 0096 8000 006b 0000 0000 0000 0000 * ...