Created
July 12, 2017 19:36
-
-
Save batoreh/306f68d7faec08a044f4e5c6b2ba524d to your computer and use it in GitHub Desktop.
createreducer.js
This file contains 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
/** | |
* 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