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

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

  2. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

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

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

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

  6. 手把手教你撸一套Redux(Redux源码解读)

    Redux 版本:3.7.2 Redux 是 JavaScript 状态容器,提供可预测化的状态管理. 说白了Redux就是一个数据存储工具,所以数据基础模型有get方法,set方法以及数据改变后通知 ...

  7. 记一次修改框架源码的经历,修改redux使得redux 可以一次处理多个action,并且只发出一次订阅消息

    redux是一个数据状态管理的js框架,redux把行为抽象成一个对象,把状态抽象成一个很大的数据结构,每次用户或者其他什么方式需要改变页面都可以理解成对数据状态的改变,根据出发这次改变的不同从而有各 ...

  8. React-安装和配置redux调试工具Redux DevTools

    chrome扩展程序里搜索Redux DevTools进行安装 新建store的时候,进行如下配置. import { createStore, applyMiddleware ,compose} f ...

  9. 25.redux回顾,redux中的action函数异步

    回顾:Redux: 类似于 Vuex 概念:store/reducer/action action:动作 {type,.....} 一定要有type 其他属性不做限制 reducer:通过计算产生st ...

随机推荐

  1. Redis封装之String

    RedisBase类 /// <summary> /// RedisBase类,是redis操作的基类,继承自IDisposable接口,主要用于释放内存 /// </summary ...

  2. jasperreport 追加新报表(2)

    用ireport做好模版后,如果要新加一个打印页,如果是新手,直接修改模版应该是理想情况, 可是什么数据源 feild,parameter,var,subreport ,还有路径, 真的可以让一个人疯 ...

  3. OPENCV(1)

    VS 程序的默认路径是源码所在路径(所以图片应该放在此处),而不是Debug路径   OpenCV 模块结构: core--定义了基本数据结构,包括最重要的Mat和一些其他的模块 imgproc--该 ...

  4. ORA-12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN> in 12.2.0 Database

    报错如下:ORA-12012: error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_88"ORA-2000 ...

  5. Debian9 ifconfig命令找不到解决办法

    Debian9 ifconfig命令找不到解决办法 ifconfig.route.arp和netstat等命令行工具(它们统称为net-tools),管理和排查各种网络配置.这类工具原先起源于BSD ...

  6. Linux VNC客户端软件VNC Viewer | RealVNC

    Linux很多时候是作为服务器操作系统,如果是桌面系统通常情况会远程管理linux服务器,很多时候通过VNC进行远程管理,这个时候就要在客户端安装VNC客户端软件,VNC Viewer | RealV ...

  7. 如何获取repeater某行第一列的值

    <div> <asp:Repeater ID="Repeater1" runat="server" DataMember="Defa ...

  8. Python学习笔记3:简单文件操作

    # -*- coding: cp936 -*- # 1 打开文件 # open(fileName, mode) # 參数:fileName文件名称 # mode打开方式 # w     以写方式打开. ...

  9. C#中函数的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. Android 通过OnScrollListener来监听RecyclerView的位置

    最近做一个漫画app,在阅读漫画界面需要通过获取recyclerView的位置来实时更新界面上的图片进度(比如1/9), 查阅资料得知了可以通过LayoutManager来获取recyclerView ...