[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 ...
随机推荐
- javascript对象如何使用
javascript对象如何使用 一.总结 一句话总结:JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... 因为函数是对象,所以自定义对象的创建中有种方法就是函数 1.js中的 ...
- syslog介绍-CS架构来采集系统日志
syslog架构 Unix/Linux系统中的大部分日志都是通过一种叫做syslog的机制产生和维护的.syslog是一种标准的协议,分为客户端和服务器端,客户端是产生日志消息的一方,而服务器端负责接 ...
- 仿即刻app"猜你喜欢"切换控件
最近在即刻里看到即刻的"猜你喜欢"的板块,觉得效果很赞. 当点击"换一换"时,上面三个条目程序切换效果,并且三个条目的切换以不同的速度进行. 于是开始想办法撸出 ...
- javascript 左侧菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 异步Ajax
Ajax异步刷新应用在Web开发中经常用到,在过去WebForm中通常是使用JQuery和一般处理程序或者aspx页面来实现: 在MVC中,虽然依然可以使用一般处理程序,但是一般还是通过在Contro ...
- Shiro架构及其组件
Shiro可以帮助我们完成:认证.授权.加密.会话管理.与Web集成.缓存等.这不就是我们想要的嘛,而且Shiro的API也是非常简单:其基本功能点如下图所示: Authentication:身份认证 ...
- vue --- axios发post请求后台接收不到参数的三种解决方案
最近用vue 做项目使用axios 发送post 请求时遇到了前端传数据后端接收不到的情况: 后来仔细对比发现axios传值是这样的: 而 ajax 传值是这样的: 一个 Request Paylo ...
- Vim使用心得总结
基本快捷键 v 进入可视模式 i / a 光标前/后插入模式 I / A 行首/末插入模式 Crtl+c 进入命令模式 Crtl+v 进入块可视模式 Q 进入EX模式 gh 进入选择模式 u 撤销 U ...
- Swift学习笔记(3)--基本运算符
基本运行符: +(加法.正数) - (减法.负数) * (乘法) / (除法) % (求余) : 在Swift中,求余可以是浮点数求余. &&(逻辑与) || (逻辑或) ^ (逻 ...
- HDU——T 1251 统计难题
http://acm.hdu.edu.cn/showproblem.php?pid=1251 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...