Skip to content

Instantly share code, notes, and snippets.

@yonahforst
Created November 2, 2018 15:17
Show Gist options
  • Save yonahforst/a18aba6a953cce073577f07f8a77f128 to your computer and use it in GitHub Desktop.
Save yonahforst/a18aba6a953cce073577f07f8a77f128 to your computer and use it in GitHub Desktop.
todo list reducer
const reducer = (state, event) => {
switch (event.type) {
case 'TodoAdded':
// append event to the list of existing events.
return [
...state,
{ title: event.title, completed: false },
]
case 'TodoRemoved':
// remove the item at given index (non-mutating).
return [
...state.slice(0, event.index),
...state.slice(event.index + 1),
]
case 'TodoCompleted':
// change item at given index (non-mutating).
return [
...state.slice(0, event.index),
{ ...state[event.index], completed: true },
...state.slice(event.index + 1),
]
default:
return state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment