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)
}
According to MDN Web Docs on const, The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.
Since const todo is an object, you can change its properties (i.e. todo.complete)