[Redux] Understand Redux Higher Order Reducers
Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a new reducer. In that new reducer, you can customize the behaviour of the original one which helps reducing the reducer logic.
In this lesson, we'll see how to reduce duplicated code by refactoring two different reducers into a higher order reducer.
Reducers:
export default (state = [], { type, payload }) => {
switch (type) {
case "ADD_ARTICLE":
return [...state, payload]
default:
return state
}
}
export default (state = [], { type, payload }) => {
switch (type) {
case "ADD_USER":
return [...state, payload]
default:
return state
}
}
They both share the same code structure.
HOC reducer:
which is a reducer hoc function return a reducer function.
import { combineReducers } from "redux"
import users from "./users"
import articles from "./articles"
const addHoc = (reducer, predicate) => (state, action) => {
if (predicate(action.type)) {
return [...state, action.payload]
}
return reducer(state, action)
}
const rootReducer = combineReducers({
users: addHoc(users, type => type === "ADD_USER"),
articles: addHoc(articles, type => type === "ADD_ARTICLE")
})
export default rootReducer
If match the predicate function, then we can compute the next state and return it. If doesn't match, then pass to the reducer normally. Then we can remove "ADD_USER" and "ADD_ARTICLE" cases from reducers.
Personally I don't think this is a good approach... even it reduce the boilerplate code, but it decrease the code readability. I still prefer keep all the reducer logic inside the its reducer file. Just make a reuseable function would be better:
export const append = (state, payload) => {
return [...state, payload]
}
export default (state = [], { type, payload }) => {
switch (type) {
case "ADD_USER":
return append(state, payload)
default:
return state
}
}
It also make Unit testings easier.
[Redux] Understand Redux Higher Order Reducers的更多相关文章
- [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions
[CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...
- [React] Higher Order Components (replaces Mixins)
Higher order components will allow you to apply behaviors to multiple React components. So the idea ...
- [React] Implement a Higher Order Component with Render Props
When making a reusable component, you'll find that people often like to have the API they're most fa ...
- [React Intl] Use a react-intl Higher Order Component to format messages
In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...
- [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that ...
- 手把手教你撸一套Redux(Redux源码解读)
Redux 版本:3.7.2 Redux 是 JavaScript 状态容器,提供可预测化的状态管理. 说白了Redux就是一个数据存储工具,所以数据基础模型有get方法,set方法以及数据改变后通知 ...
- 记一次修改框架源码的经历,修改redux使得redux 可以一次处理多个action,并且只发出一次订阅消息
redux是一个数据状态管理的js框架,redux把行为抽象成一个对象,把状态抽象成一个很大的数据结构,每次用户或者其他什么方式需要改变页面都可以理解成对数据状态的改变,根据出发这次改变的不同从而有各 ...
- React-安装和配置redux调试工具Redux DevTools
chrome扩展程序里搜索Redux DevTools进行安装 新建store的时候,进行如下配置. import { createStore, applyMiddleware ,compose} f ...
- 25.redux回顾,redux中的action函数异步
回顾:Redux: 类似于 Vuex 概念:store/reducer/action action:动作 {type,.....} 一定要有type 其他属性不做限制 reducer:通过计算产生st ...
随机推荐
- C++ BigInteger模板
#include <cstdio> #include <cstring> #include <string> #include <iostream> # ...
- update-alternatives 命令
update-alternatives 命令 1.功能作用 update-alternatives是dpkg的实用工具,用来维护系统命令的符号链接,以决定系统默认使用什么命令. 在Debian系统中, ...
- rem的解决方案
http://www.qdfuns.com/notes/32967/37b8f127797c6c2b66f2c1dc6d133e45.html rem 作为一个低调的长度单位,由于手机端网页的兴起,在 ...
- css3 文字溢出 换行实现方案
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux和Windows双系统让 Windows 把硬件时间当作 UTC
linux和Windows双系统让 Windows 把硬件时间当作 UTC Windows设置如下:开 始->运行->CMD,打开命令行程序(Vista则要以管理员方式打开命令行程序方可有 ...
- perl编程问题
一.Hash类型 1.hash遍历输出:如果hash遍历输出的时候不是按key则会按数组输出. my %hash=(); ${hash}{"a"}="1"; $ ...
- Html学习总结(1)——理解Html的head
HTML文档的head部分,通常包括指定页面标题,为搜索引擎提供关于页面本身的信息,加载样式表,以及加载JavaScript文件(出于性能考虑,多数时候放在页面底部</body>标签结束前 ...
- 使用bitmap处理海量数据
bitmap是一个十分实用的结构.所谓的Bit-map就是用一个bit位来标记某个元素相应的Value, 而Key即是该元素.因为採用了Bit为单位来存储数据,因此在存储空间方面,能够大大节省. 适 ...
- SQLite详解,案例,手册
SQLite 存储类型 1.NULL 2.INTEGER 3.REAL 4.TEXT 5.BLOB 创建表 CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT N ...
- 关于exports 和 module.exports
本文来源为node.js社区附上链接 http://cnodejs.org/topic/5231a630101e574521e45ef8 require 用来加载代码,而 exports 和 modu ...