Skip to content

Instantly share code, notes, and snippets.

@jagretz
Created April 18, 2018 00:43
Show Gist options
  • Save jagretz/b9ad029c796faf38092e403e8b6154c4 to your computer and use it in GitHub Desktop.
Save jagretz/b9ad029c796faf38092e403e8b6154c4 to your computer and use it in GitHub Desktop.
Reduce multiple reducer functions passing state and action through each in sequence.
/**
* Supply multiple reducer functions, as arguments, and return a new reducer function that pipes the
* state and action through each reducer in sequence.
* @param {...functions} reducers like (arg1, arg2, ...), as a serires of arguments.
* @return {function} a new reducer function that pipes the state and action through each reducer in sequence.
* Each successive reducer will receive the updated returned by the previous reducer.
*/
export default function reduceReducers(...reducers) {
return (currentState = {}, action = {}) =>
reducers.reduce((updatedState, red) => red(updatedState, action), currentState);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment