Skip to content

Instantly share code, notes, and snippets.

@pokorson
Last active April 9, 2017 22:15
Show Gist options
  • Save pokorson/bc774bf3135e27e8617250e5cc244b99 to your computer and use it in GitHub Desktop.
Save pokorson/bc774bf3135e27e8617250e5cc244b99 to your computer and use it in GitHub Desktop.
Redux helpers
const createAction = (type) => {
return (payload) => ({type, payload})
}
const createReducer = (initState, handlers) => {
return (state = initState, action) => {
const actionHandler = handlers[action.type];
if (actionHandler) {
const result = actionHandler(state, action.payload);
return Object.assign({}, state, result);
}
return state;
};
};
// Example usage
const increment = createAction('INCREMENT');
const postsReducer = createReducer({count: 0]}, {
'INCREMENT': (state, incrementNumber) => ({count: state.count + incrementNumber})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment