[React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions
We only have a few dispatching functions that need to be known by our React Application. Each one actually has multiple actions that need to be dispatched. While we could just have many imperative calls to dispatch in our dispatching functions, but why not use it as an excuse to use an array and write some middleware.
We will create a middleware function that will check dispatched actions to see if they are arrays. If a given action is an array we loop over the array, dispatching each action in turn. If it is not however, we just pass it along to be handled downstream.
Create a middle which can take dispatch fns as array type:
function multiMiddleware({ dispatch }) {
return next => action => {
return isSameType(Array, action)
? action.forEach(a => dispatch(a))
: next(action);
};
}
Apply the middle:
import { createStore, compose, applyMiddleware } from "redux";
function multiMiddleware({ dispatch }) {
return next => action => {
return isSameType(Array, action)
? action.forEach(a => dispatch(a))
: next(action);
};
}
const middleware = applyMiddleware(multiMiddleware);
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
export default createStore(
reducer,
initialState(),
composeEnhancers(middleware)
);
Previously, we can only apply one dispatch fn:
start: () => dispatch(startGame())
Now we can dispatch multi actions:
start: () => dispatch([startGame(), hideAllCards()])
[React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions的更多相关文章
- [React + Functional Programming ADT] Create Redux Middleware to Dispatch Actions with the Async ADT
We would like the ability to group a series of actions to be dispatched with single dispatching func ...
- [React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
With our Redux implementation lousy with State ADT based reducers, it is time to hook it all up to a ...
- [Functional Programming ADT] Create a Redux Store for Use with a State ADT Based Reducer
With a well defined demarcation point between Redux and our State ADT based model, hooking up to a R ...
- [Functional Programming ADT] Initialize Redux Application State Using The State ADT
Not only will we need to give our initial state to a Redux store, we will also need to be able to re ...
- [Functional Programming ADT] Create State ADT Based Reducers (applyTo, Maybe)
The typical Redux Reducer is function that takes in the previous state and an action and uses a swit ...
- [Functional Programming ADT] Adapt Redux Actions/Reducers for Use with the State ADT
By using the State ADT to define how our application state transitions over time, we clear up the ne ...
- [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers
Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...
- [Functional Programming ADT] Debug a Functional JavaScript composeK Flow
When using ADTs in our code base, it can be difficult to use common debugging tools like watches and ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
随机推荐
- highcharts高级画图柱状图和折线图
折线图一枚 $("#z_line").highcharts({ chart: { type: 'line' }, credits: { enabled: false // 禁用版权 ...
- lvm笔记
安装LVMyum -y install lvm* 创建PV# pvcreate /dev/md5 /dev/sdf1 /dev/sdg 查看PV# pvdisplay 还可以使用命令pvs 和pvsc ...
- 四十四 常用内建模块 struct
准确地讲,Python没有专门处理字节的数据类型.但由于str既是字符串,又可以表示字节,所以,字节数组=str.而在C语言中,我们可以很方便地用struct.union来处理字节,以及字节和int, ...
- Python语法31[module/package+import]
一 module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc" ...
- ubuntu下做柯老师lab19-lab20实验问题总结
前两篇文章告诉了大家如何将无线封包传输遗失模型和myevalvid添加到ns2.35中,已经成功验证了,这个没有问题.但是本人在做lab19和lab20实验时又发现了一些关于myevalvid工具集的 ...
- centos系统mysql数据库忘记密码重置方法(ERROR 1045 28000 Access denied...)
当mysql的密码错误的时候,就会报如下这样的错误信息 解决方法如下: 首先输入mysqld_safe --skip-grant-tables 然后停止mysql服务,输入service mysqld ...
- RabbitMQ (十) 远程过程调用(RPC)
在远程计算机上运行一个函数并等待结果,我们通常叫这种模式为远程过程调用或者RPC. 通过 RabbitMQ 进行 RPC 很容易,客户端发送请求消息,服务器回复响应消息.为了接收响应,我们需要发送带有 ...
- CXF浅析
CXF 框架支撑环境 CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架本身之外,还需要 JDK 和 Ser ...
- 【二分】【线段树】hdu6070 Dirt Ratio
size(l,r)表示区间l,r权值的种类数,让你求min{size(l,r)/(r-l+1)}(1<=l<=r<=n). last[r]表示a[r]上一次出现的位置, 就是二分验证 ...
- CSS写作建议和性能优化总结(未完待续)
这里是我从网上的一篇文章看过来的,这里先做一点小结,之后再补充. 1.CSS渲染规则 今天在微博的一篇文章上看到的,之前我都以为渲染是从左往右渲染.发现我的想法是错的.之所以采用从右往左的渲染规则,是 ...