Skip to content

Instantly share code, notes, and snippets.

@joshwcomeau
Last active August 11, 2016 12:58
Show Gist options
  • Save joshwcomeau/e23919aab62121ee966e0b66e4c6bffe to your computer and use it in GitHub Desktop.
Save joshwcomeau/e23919aab62121ee966e0b66e4c6bffe to your computer and use it in GitHub Desktop.
Disappointing Ducks
const noop = function noop() {};
export default function constructReducer(initialState, caseCreator = noop) {
// Because this function creates a reducer, it needs to return a reducer.
return (state = initialState, action) => {
// caseCreator is a function that returns an object, representing all
// of our former cases in the switch statement. It's a function so that
// the cases have access to the state and the action.
const cases = caseCreator(state, action);
// The expressions for each case will have been resolved to static values.
// we can simply select the one that matches our action's type.
const newState = cases[action.type];
// We still need to cover our former 'default' case.
// Simply return the original state if newState is undefined.
return typeof newState !== 'undefined' ? newState : state;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment