Skip to content

Instantly share code, notes, and snippets.

@HenrikJoreteg
Last active August 24, 2016 14:37
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 HenrikJoreteg/5e84f0b9d709b0c93abf0a458cbf54d5 to your computer and use it in GitHub Desktop.
Save HenrikJoreteg/5e84f0b9d709b0c93abf0a458cbf54d5 to your computer and use it in GitHub Desktop.
reducer pattern with blocks
import { DO_THING, DO_OTHER_THING, SOMETHING_ELSE } from './actions'
export default (state, { type, payload }) => {
switch (type) {
case DO_THING: {
// by using blocks in our case statements we can create locally scoped vars
// and returning early keeps things readable
const scopedVariable = payload.thing
return Object.assign({}, state, {do: scopedVariable})
}
case DO_OTHER_THING: {
return Object.assign({}, state, {doOther: 'thing'})
}
case SOMETHING_ELSE: {
return Object.assign({}, state, {something: 'else'})
}
}
return state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment