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的更多相关文章

  1. [Redux] Reducer Composition with combineReducers()

    Previous, we do composition with objects: const todoApp = (state = {}, action) => { return { todo ...

  2. 关联,聚合和组合(复合)--Association, Aggregation and Composition

    概要 Association, Aggregation and Composition are terms that represent relationships among objects. Th ...

  3. 【OOAD】面向对象设计原则概述

    软件的可维护性和可复用性 知名软件大师Robert C.Martin认为一个可维护性(Maintainability) 较低的软件设计,通常由于如下4个原因造成: 过于僵硬(Rigidity)  ...

  4. Java Programming Guidelines

    This appendix contains suggestions to help guide you in performing low-level program design and in w ...

  5. DVB-subtitle解析流程浅

    DTV包含SUBTITLE和TTX. PMT中分别有不同的描述符对应,如下图的TTX descripter=0x56.语言ISO-639="fin" subtitle descri ...

  6. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  7. [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 ...

  8. Redux源码学习笔记

    https://github.com/reduxjs/redux 版本 4.0.0 先了解一下redux是怎么用的,此处摘抄自阮一峰老师的<Redux 入门教程> // Web 应用是一个 ...

  9. redux源码解析-函数式编程

    提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript> ...

随机推荐

  1. Cacti添加threshold、monitor和setting

    Cacti版本:Version 0.8.8b 一.插件介绍: monitor:通过简单明了的图标提供服务器的运行状态 settings:给不同的插件提供一些共用的信息,如邮件信息,dns信息thold ...

  2. windows下安装CI框架

    CI框架是一个非常流行的 mvc框架, CI框架如何安装和使用,在CI中文网已经讲的比较详细了 ,这里记录下几个需要注意的地方. 一. index.php问题 把压缩包下载解压到项目根目录即可运行里面 ...

  3. C++ 性能剖析 (三):Heap Object对比 Stack (auto) Object

    通常认为,性能的改进是90 ~ 10 规则, 即10%的代码要对90%的性能问题负责.做过大型软件工程的程序员一般都知道这个概念. 然而对于软件工程师来说,有些性能问题是不可原谅的,无论它们属于10% ...

  4. 开始编写正式的iOS 程序(iOS编程指导)

    App设计基础 在确定了你的App主要功能后,需要把它转化为代码.如果你是第一次开发属于自己的iOS App,需要花些时间熟悉基本概念.iOS内置了很多设计样式,多了解下能对你以后有帮助. 初稿 设计 ...

  5. 『重构--改善既有代码的设计』读书笔记----Extract Method

    在编程中,比较忌讳的一件事情就是长函数.因为长函数代表了你这段代码不能很好的复用以及内部可能出现很多别的地方的重复代码,而且这段长函数内部的处理逻辑你也不能很好的看清楚.因此,今天重构第一个手法就是处 ...

  6. Android学习----Android架构

    android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层.蓝色的代表java程序,黄色的代码为运行JAVA程序而实现的虚拟机,绿色部分为C/C++语言编写 ...

  7. Jquery中$.post()与$.get()区别

    1:GET访问 浏览器 认为 是等幂的 就是 一个相同的URL 只有一个结果[相同是指 整个URL字符串完全匹配] 所以 第二次访问的时候 如果 URL字符串没变化 浏览器是 直接拿出了第一次访问的结 ...

  8. python运维开发之第八天(socket)

    什么是 Socket? Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. soc ...

  9. C++学习笔记4——类的封装(2)

    简介: 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.其中一元运算符有一个参数,二元运算符有两个参数. 可以被重载的运算符 + - * / % ^ ...

  10. SQL Server分区动态生成脚本(三)(按年份划分)

    --生成分区脚本DECLARE @DataBaseName NVARCHAR(50)--数据库名称DECLARE @TableName NVARCHAR(50)--表名称DECLARE @Column ...