Skip to content

Instantly share code, notes, and snippets.

@Arjun-sna
Last active June 24, 2018 06:25
Show Gist options
  • Save Arjun-sna/e246811cd1834ef1edda7e13b57675e4 to your computer and use it in GitHub Desktop.
Save Arjun-sna/e246811cd1834ef1edda7e13b57675e4 to your computer and use it in GitHub Desktop.
Gist to create reducer in redux
export function createReducer(reducerMap, defaultState) {
return (state = { ...defaultState }, action) => reducerMap.hasOwnProperty(action.type) ? reducerMap[action.type](state, action) : state;
}
// Sample usage
posts: createReducer({
[ALL_POSTS_RECEIVED]: (state, action) => {
//do something
},
[POSTS_REQUEST_IN_PROGRESS]: (state, action) => {
//do something
}
}, { posts: [], moreDataAvailable: true }),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment