Skip to content

Instantly share code, notes, and snippets.

@granmoe
Last active May 9, 2017 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save granmoe/f91fe34beb3285a964a975337ca1b077 to your computer and use it in GitHub Desktop.
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.
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
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