Sub topic 7
Toggling and deleting without mutating state
This chapter assumes you know useState, controlled inputs, and rendering lists with keys. It also assumes you know the array methods map and filter and the spread syntax. You will add toggle and delete behavior to the to-do list and learn why React needs new objects and arrays instead of edited ones.
Why mutation breaks React
When you call a state setter, React compares the new value with the old one using Object.is. If both are the same reference, React concludes nothing changed and may skip the re-render.
Mutating state in place causes exactly this problem. The array is the same object after push or after you edit todo.done, so the screen can stay out of date even though the data changed.
// Wrong: the array reference does not change
function toggleTodo(id) {
const todo = todos.find((t) => t.id === id);
todo.done = !todo.done;
setTodos(todos);
}The fix is to treat state as read-only. You build a new array, and a new object for each item that changes.
Toggling with map
map returns a new array. For the matching item, return a copy made with spread syntax and a flipped done. Return every other item unchanged.
function toggleTodo(id) {
setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
}Unchanged items keep their old references. Only the toggled item and the array are new, so React can see exactly what changed.
Deleting with filter
filter returns a new array that keeps only the items for which the callback returns true. To delete, keep everything whose id differs from the target.
function deleteTodo(id) {
setTodos(todos.filter((t) => t.id !== id));
}Avoid splice and pop here. Both change the original array.
Wiring the handlers into TodoItem
The parent owns the state, so TodoItem receives callbacks as props and calls them with the item id.
function TodoItem({ todo, onToggle, onDelete }) {
return (
<li>
<label>
<input
type="checkbox"
checked={todo.done}
onChange={() => onToggle(todo.id)}
/>
<span style={{ textDecoration: todo.done ? "line-through" : "none" }}>
{todo.title}
</span>
</label>
<button type="button" onClick={() => onDelete(todo.id)}>
Delete
</button>
</li>
);
}In the list, pass the handlers down:
<ul>
{todos.map((todo) => (
<TodoItem
key={todo.id}
todo={todo}
onToggle={toggleTodo}
onDelete={deleteTodo}
/>
))}
</ul>Because each item has a stable key, React keeps the right component for each to-do after a deletion.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| `todo.done = !todo.done` | UI may not update | Copy with `{ ...t, done: !t.done }` |
| `todos.push(item)` then `setTodos(todos)` | Same reference, no re-render | `setTodos([...todos, item])` |
| Calling `onToggle(todo.id)` directly in JSX | Runs during render | Wrap it: `() => onToggle(todo.id)` |
| Using the array index as key | Wrong state after a delete | Use `todo.id` |
Exercise
Exercise
Write a function clearCompleted that removes every finished to-do without mutating state.
Show answer
function clearCompleted() { setTodos(todos.filter((t) => !t.done)); } The filter call returns a new array that keeps only the unfinished items, so React sees a new reference and re-renders.