Skip to content

Instantly share code, notes, and snippets.

@tleunen
Last active March 16, 2018 15:20
Show Gist options
  • Save tleunen/c10bcdb1f225f08316d49ad57bb13d3d to your computer and use it in GitHub Desktop.
Save tleunen/c10bcdb1f225f08316d49ad57bb13d3d to your computer and use it in GitHub Desktop.
Easy create a redux reducer
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;
};
}
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