Skip to content

Instantly share code, notes, and snippets.

@mcjcloud
Created July 10, 2020 06:43
Show Gist options
  • Save mcjcloud/31a7412e8e7feafa2ff6d401add8719f to your computer and use it in GitHub Desktop.
Save mcjcloud/31a7412e8e7feafa2ff6d401add8719f to your computer and use it in GitHub Desktop.
Asterisk Medium Article: reducer
// Reducer
const reducer = (state: TodoState = defaultState, action: TodoAction): TodoState => {
switch (action.type) {
...
case "TODO_UNCOMPLETED_STARTED": {
return { ...state, isUncompletingTodo: true }
}
case "TODO_UNCOMPLETED": {
return {
...state,
isCompletingTodo: false,
todos: state.todos.reduce((todos, nextTodo) => {
if (nextTodo._id === action.payload.todo._id) {
return [...todos, action.payload.todo]
}
return [...todos, nextTodo]
}, [] as TodoItem[]),
}
}
default: {
return state
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment