Last active
March 16, 2018 15:20
-
-
Save tleunen/c10bcdb1f225f08316d49ad57bb13d3d to your computer and use it in GitHub Desktop.
Easy create a redux reducer
This file contains hidden or 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
export default function createReducer(initialState, handlers) { | |
return function reducer(state = initialState, action) { | |
let nextState = state; | |
if ({}.hasOwnProperty.call(handlers, '*')) { | |
nextState = handlers['*'](nextState, action); | |
} | |
if ({}.hasOwnProperty.call(handlers, action.type)) { | |
return handlers[action.type](nextState, action); | |
} | |
return nextState; | |
}; | |
} |
This file contains hidden or 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
export default createReducer(initialState, { | |
'my-action': updateSomething | |
'*': runOnAllActions | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment