mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
Fixing the complete reducer to actually create new instance of a todo item
This commit is contained in:
@@ -15,13 +15,9 @@ export function remove(state: Store['todos'], id: string) {
|
||||
}
|
||||
|
||||
export function complete(state: Store['todos'], id: string) {
|
||||
// Clone the todos
|
||||
const newTodos = { ...state };
|
||||
|
||||
// Manipulate the completed flag
|
||||
newTodos[id].completed = !newTodos[id].completed;
|
||||
|
||||
return newTodos;
|
||||
// Clone the todo, overriding
|
||||
const newTodo = { ...state[id], completed: !state[id].completed };
|
||||
return { ...state, [id]: newTodo };
|
||||
}
|
||||
|
||||
export function clear(state: Store['todos']) {
|
||||
|
||||
@@ -20,8 +20,8 @@ export function remove(state: Store['todos'], id: string) {
|
||||
|
||||
export function complete(state: Store['todos'], id: string) {
|
||||
// Write code:
|
||||
// - to clone the state object into new state object
|
||||
// - create a clone of the state[id] into a new item object
|
||||
// - to clone the state[id] object into new todo object, using the spread syntax
|
||||
// - in the spread syntax, also override the value of the completed key like this: {...foo, [id]: !foo[id].completed}
|
||||
// - modify new state and set the id key to the value of the new item object
|
||||
|
||||
return state;
|
||||
|
||||
Reference in New Issue
Block a user