Created
May 28, 2017 20:50
-
-
Save joedski/307248985d367af808bb02d5195b6e5d to your computer and use it in GitHub Desktop.
Javascript: Redux: Dispatching Multiple Actions: Custom Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First, our action creator. | |
const MULTI_ACTION = 'multiAction/MULTI_ACTION'; | |
const multiAction = (actions, meta = {}) => ({ | |
type: MULTI_ACTION, | |
payload: { actions }, | |
meta, | |
}); | |
// Next, the middleware itself. | |
const multiActionMiddleware = store => next => action => { | |
switch (action.type) { | |
case MULTI_ACTION: { | |
next(action); | |
const results = action.payload.actions.map(a => store.dispatch(a)); | |
return results; | |
} | |
default: return next(action); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment