[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 ...
随机推荐
- pandas 下的 one hot encoder 及 pd.get_dummies() 与 sklearn.preprocessing 下的 OneHotEncoder 的区别
sklearn.preprocessing 下除了提供 OneHotEncoder 还提供 LabelEncoder(简单地将 categorical labels 转换为不同的数字): 1. 简单区 ...
- SQLite: sqlite_master(转)
转自:http://blog.sina.com.cn/s/blog_6afeac500100yn9k.html SQLite数据库中一个特殊的名叫 SQLITE_MASTER 上执行一个SELECT查 ...
- JAVA使用Gson解析json数据,实例
封装类Attribute: public class Attribute { private int id; private String name; private int age; public ...
- Bundles软件
Bundle 称为:软件集 或 打包捆绑软件(软件束) Bundle就是一组包含了文件集,软件包或许可程序产品的软件,它们组合在一起为了实现一个特定的功能 快速来列出系统bundle软件 sm ...
- openSUSE leap 42.3 实现有线 无线同时用
因为工作的原因,经常会用有线网卡连接服务器进行配置,无线网卡上外网. 一.查看当前网关信息 pipci@openSUSE:~> ip route show 可以看到前两行default开头的就是 ...
- JSP页面的静态包含和动态包含的区别与联系
JSP中有两种包含: 静态包含:<%@include file="被包含页面"%> 动态包含:<jsp:include page="被包含页面" ...
- which---查找并显示给定命令的绝对路径
which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用which命令,就可 ...
- PHP解析XML格式文档
<?php// 首先要建一个DOMDocument对象$xml = new DOMDocument();// 加载Xml文件$xml->load("3.xml");// ...
- CCF模拟题 最优灌溉
最优灌溉 时间限制: 1.0s 内存限制: 256.0MB 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉. 为了灌溉,雷雷需 ...
- Android 4.4 Fence在SurfaceFlinger中的应用
网上关于android.fence的资料好少啊.差点儿没有,可是这个机制又在GUI系统中起着关键的数据,于是自己通读源代码和凝视.与大家分享下Fence究竟是怎么回事? Fence即栅栏.栅栏的角色与 ...