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

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

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

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

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

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

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

  7. [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers

    Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...

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

  9. 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 ...

随机推荐

  1. [水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API

    问题 怎么样将 Asp.Net Web Api 加入到 Asp.Net Web From 应用程序中 解决方案 在 Visual Studio 2013 中,创建新的 Web From,可以直接在&q ...

  2. 我的第一个web开发框架

    怎么才能成为一名架构师?需要具备哪些条件? 作为一名码农我迫切希望自己成为一个比较合格的web架构师,昨晚心血来潮小弟花了4个小时的时间整了个简易的web开发框架,本着开源的精神做个分享,希望和更多的 ...

  3. Poj1733 Parity Game(带权并查集)

    题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...

  4. Python开发基础-Day8-装饰器扩展和迭代器

    wraps模块 让原函数保留原来的说明信息 import time import random from functools import wraps def auth(func): '''auth ...

  5. Ubuntu用户管理原理

    Ubuntu账户: Ubuntu有三类账户:超级用户.普通用户以及系统用户. 每一个用户在ubuntu中都必须拥有一种账户,在Ubuntu中, /etc/passwd用来保存每个账户的信息.实际密码保 ...

  6. luogu P1012 拼数

    题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...

  7. BZOJ 3127 [Usaco2013 Open]Yin and Yang(树点分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3127 [题目大意] 给出一棵01边权树,求存在多少条路径,使得路径上0和1的数量相同, ...

  8. [LOJ2541]猎人杀

    好题== 先转化一下,每个人被杀死后给他打标记,以后随机杀人时选到有标记的人就继续随机,这样并不会改变每个人被杀死的概率 直接算很难算,考虑容斥,我们枚举$S$表示在$1$死后才死的人至少在集合$S$ ...

  9. LRU近期最少使用算法

    LRU(least recently used)最少使用. 假设 序列为 4 3 4 2 3 1 4 2 物理块有3个 则 首轮 4调入内存 4 次轮 3调入内存 3 4 之后 4调入内存 4 3 之 ...

  10. Android Logcat Security(转)

    讲解了在Android开发中logcat使用不当导致的安全问题 原帖地址:http://drops.wooyun.org/tips/3812 0x00 科普 development version : ...