I'm trying to understand why this example is a good idea.
I'm learning React and I found this function which changes the complete
property from todo
.
My question is why this would work? Because we have the const todo
which is the one being changed in reality, not the newTodoList
which is the one that we return back. I'm guessing that the respective todo
from newTodoList
is changed as well somehow but I don't undertand how this works because again, we changed the newly created const todo
function toggleTodo(id){
const newTodoList = [...todos]
const todo = newTodoList.find(todo => todo.id === id)
todo.complete = !todo.complete
setTodos(newTodoList)
}