Last active
May 9, 2017 01:50
-
-
Save granmoe/f91fe34beb3285a964a975337ca1b077 to your computer and use it in GitHub Desktop.
A simple pattern for having a reducer as a map of functions instead of a switch statement. One benefit is being able to grab any of the reducer's functions independently for testing.
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 (reducerMap = {}, initialState) => | |
(state = initialState, { type = '', ...payload }) => | |
(reducerMap[type] || (() => state))(state, payload) // if type isn't found in the reducer, just use a noop |
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
import Immutable from 'immutable' | |
import makeReducer from './make-reducer' | |
const INITIAL_STATE = Immutable.Map() | |
export default makeReducer({ | |
'users/update-users': (state, { users }) => { | |
return Immutable.fromJS(users) | |
}, | |
'users/update-user': (state, { user, id }) => { | |
return state.set(id, user) | |
}) | |
}, INITIAL_STATE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment