Created
November 2, 2018 15:17
-
-
Save yonahforst/a18aba6a953cce073577f07f8a77f128 to your computer and use it in GitHub Desktop.
todo list reducer
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
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