[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 ...
随机推荐
- 六十一 Web开发 使用Web框架
由于用Python开发一个Web框架十分容易,所以Python有上百个开源的Web框架.这里我们先不讨论各种Web框架的优缺点,直接选择一个比较流行的Web框架——Flask来使用. 用Flask编写 ...
- SpringMVC组件配置
web.xml . springmvc-servlet.xml 配置SpringMVC四大组件. web.xml 配置前端控制器:前端控制器就是个servlet <!-- 配置前端控制器 --& ...
- 转:GitHub 万星推荐成长技术清单
转:http://www.4hou.com/info/news/7061.html 最近两天,在reddit安全板块和Twitter上有个GitHub项目很火,叫“Awesome Hacking”. ...
- CodeForces 732F Tourist Reform
边双连通分量. 这题有一点构造的味道.一个有向图,经过强连通缩点之后会形成一个有向无环图. 如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图. 接下来 ...
- 决策树:ID3与C4.5算法
1.基本概念 1)定义: 决策树是一个预测模型:他代表的是对象属性与对象值之间的一种映射关系,树中每个节点代表的某个可能的属性值. 2)表示方法: 通过把实例从根结点排列到某个叶子结点来分类实例,叶子 ...
- Bzoj 2286 & Luogu P2495 消耗战(LCA+虚树+欧拉序)
题面 洛谷 Bzoj 题解 很容易想到$O(nk)$的树形$dp$吧,设$f[i]$表示处理完这$i$颗子树的最小花费,同时再设一个$mi[i]$表示$i$到根节点$1$路径上的距离最小值.于是有: ...
- 【20181019T3】比特战争【最小生成树思想】
题面 [错解] Hmm不可做啊 要不按b排个序? 然后并查集瞎搞,刷刷刷过了样例 然后大样例大了几万倍 出了组小数据,Successful Hack 弃疗 水过10分 [正解] 用占领的边将顶点连起来 ...
- [BZOJ 2668] 交换棋子
Link: BZOJ 2668 传送门 Solution: 重点在于对于每条转移路径:首尾算一次,中间节点算两次 可以一点拆三点,将原流量拆成入流量和出流量 但其实也可以就拆两点,分前后是否是一首尾点 ...
- 【斜率优化】Average
[UVa1451]Average 算法竞赛入门经典第8章8-9 ( P243 ) 题目大意:给定一个长度为N的01串,选择一个长度至少为L的连续子串,使序列平均值最大 (N<=100001) 题 ...
- [BZOJ3529]数表
假设$n\leq m$,我们先不考虑$\leq a$的限制 $\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sigma((i,j))=\sum\limits_{T=1} ...