Skip to content

Instantly share code, notes, and snippets.

@batoreh
Created July 12, 2017 19:36
Show Gist options
  • Save batoreh/306f68d7faec08a044f4e5c6b2ba524d to your computer and use it in GitHub Desktop.
Save batoreh/306f68d7faec08a044f4e5c6b2ba524d to your computer and use it in GitHub Desktop.
createreducer.js
/**
* Creates a reducer.
* @param {string} initialState - The initial state for this reducer.
* @param {object} handlers - Keys are action types (strings), values are reducers (functions).
* @return {object} A reducer object.
*/
export default (initialState = null, handlers = {}) => (state = initialState, action) => {
if (!action && !action.type) return state;
const handler = handlers[action.type];
return handler && handler(state, action) || state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment