Skip to content

Instantly share code, notes, and snippets.

@joedski
Created May 28, 2017 20:50
Show Gist options
  • Save joedski/307248985d367af808bb02d5195b6e5d to your computer and use it in GitHub Desktop.
Save joedski/307248985d367af808bb02d5195b6e5d to your computer and use it in GitHub Desktop.
Javascript: Redux: Dispatching Multiple Actions: Custom Middleware
// 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