Created
July 10, 2020 06:43
-
-
Save mcjcloud/31a7412e8e7feafa2ff6d401add8719f to your computer and use it in GitHub Desktop.
Asterisk Medium Article: 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
// 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