Add to and change a todo list without mutating it: [...array, item] to add, map to change one item.
React · beginner · 8 min · 28 steps · free
import { useState } from 'react';
let nextId = 3;
export default function TodoList() {
const [todos, setTodos] = useState([
{ id: 1, text: 'Learn JSX', done: true },
{ id: 2, text: 'Learn state', done: false },
]);
function addTodo() {
setTodos([...todos, { id: nextId++, text: 'Learn effects', done: false }]);
}
function toggle(id) {
setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
}
return (
<div>
<ul>
{todos.map((t) => (
<li key={t.id}>
<input type="checkbox" checked={t.done} onChange={() => toggle(t.id)} />
{t.text}
</li>
))}
</ul>
<button onClick={addTodo}>Add</button>
</div>
);
}<TodoList> for the first time: it calls the TodoList function.useState runs for the first time, so React stores the starting value for todos and returns it: [{ id: 1, text: "Learn JSX", done: true }, { id: 2, text: …].key={t.id} keeps each row matched to its todo.t = { id: 2, text: "Learn state", done: false }, the callback returns <li>…</li>.TodoList returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.onClick handler.[...todos, newTodo] builds a new array: all the old items plus one more.<TodoList> again (render 2): it calls TodoList once more, from the top.useState returns the value React has kept for todos: [{ id: 1, text: "Learn JSX", done: true }, { id: 2, text: …]. The starting value is only used on the first render.t = { id: 1, text: "Learn JSX", done: true }, the callback returns <li>…</li>.t = { id: 2, text: "Learn state", done: false }, the callback returns <li>…</li>.t = { id: 3, text: "Learn effects", done: false }, the callback returns <li>…</li>.TodoList returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.toggle(2).t = { id: 1, text: "Learn JSX", done: true }, the callback returns { id: 1, text: "Learn JSX", done: true }.setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t))) does not change todos right away: React queues the update and will render again. In this call todos is still [{ id: 1, text: "Learn JSX", done: true }, { id: 2, text: …].toggle(t.id).<TodoList> again (render 3): it calls TodoList once more, from the top.useState returns the value React has kept for todos: [{ id: 1, text: "Learn JSX", done: true }, { id: 2, text: …]. The starting value is only used on the first render.t = { id: 1, text: "Learn JSX", done: true }, the callback returns <li>…</li>.t = { id: 2, text: "Learn state", done: true }, the callback returns <li>…</li>.t = { id: 3, text: "Learn effects", done: false }, the callback returns <li>…</li>.TodoList returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.Answer: setList([...list, x])
Answer: list.filter(t => t.id !== 2). splice changes the array.
← Updating Objects in State · Lifting State Up →