Avoiding Object mutations with Object.assign() and ...spread
Shows the naive toggleTodo implementation
const toggleTodo = (todo) => {
todo.completed = !todo.completed;
return todo;
}
Instead, to not to mutate, the object, but still keep the function generic:
return Object.assign({}, todo, {
completed: !todo.completed
});
Here (above), he passes an empty object to assign, to make sure to not to mutate the data (Object.assign({}...
. Then you add several objects to merge into this empty object. Here there's
todo
and the property we want to override.