Last active
June 24, 2018 06:25
-
-
Save Arjun-sna/e246811cd1834ef1edda7e13b57675e4 to your computer and use it in GitHub Desktop.
Gist to create reducer in redux
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
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