Step 1: The finished task row
TaskItem now receives the whole task, plus two functions to call when the checkbox is clicked or the β is pressed. It still doesn't own any data: it just reports what happened.
import type { Task } from "../types";
type Props = {
task: Task;
onToggle: (id: number) => void;
onDelete: (id: number) => void;
};
// One row in the list: a checkbox, the title, and a delete button.
export function TaskItem({ task, onToggle, onDelete }: Props) {
return (
<li className={task.done ? "task done" : "task"}>
<label>
<input type="checkbox" checked={task.done} onChange={() => onToggle(task.id)} />
<span>{task.title}</span>
</label>
<button onClick={() => onDelete(task.id)} aria-label={`Delete ${task.title}`}>
β
</button>
</li>
);
}Step 2: Show the whole list with map
import type { Task } from "../types";
import { TaskItem } from "./TaskItem";
type Props = {
tasks: Task[];
onToggle: (id: number) => void;
onDelete: (id: number) => void;
};
// Shows every task, or a friendly message when there are none.
export function TaskList({ tasks, onToggle, onDelete }: Props) {
if (tasks.length === 0) {
return <p className="empty">No tasks to show.</p>;
}
return (
<ul className="task-list">
{tasks.map((task) => (
<TaskItem key={task.id} task={task} onToggle={onToggle} onDelete={onDelete} />
))}
</ul>
);
}tasks.map(...)turns each task into a<TaskItem />. React draws the resulting list.key={task.id}is React's name tag for each row, so it knows which row is which when one is removed.- The early
returnshows a message instead of an empty list. That's conditional rendering.
Three ways to show things conditionally
| Way | Use it when | Example |
|---|---|---|
| if + early return | A whole different output | if (tasks.length === 0) return <p>No tasks to show.</p>; |
| && | Show something or nothing | {error && <p className="error">{error}</p>} |
| ? : | Choose between two things | {loading ? <p>Loadingβ¦</p> : <TaskList β¦ />} |
Step 3: Filter buttons
import type { Filter } from "../types";
type Props = {
filter: Filter;
onChange: (filter: Filter) => void;
};
const FILTERS: Filter[] = ["all", "active", "done"];
// Three buttons: All, Active, Done. The active one is highlighted.
export function FilterBar({ filter, onChange }: Props) {
return (
<div className="filters">
{FILTERS.map((f) => (
<button key={f} className={f === filter ? "selected" : ""} onClick={() => onChange(f)}>
{f[0].toUpperCase() + f.slice(1)}
</button>
))}
</div>
);
}Step 4: Put it together in App
App owns the state and passes data and functions down. Replace src/App.tsx:
import { useState } from "react";
import type { Filter, Task } from "./types";
import { TaskForm } from "./components/TaskForm";
import { TaskList } from "./components/TaskList";
import { FilterBar } from "./components/FilterBar";
export default function App() {
const [tasks, setTasks] = useState<Task[]>([]);
const [filter, setFilter] = useState<Filter>("all");
function addTask(title: string) {
setTasks([...tasks, { id: Date.now(), title, done: false }]);
}
function toggleTask(id: number) {
setTasks(tasks.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
}
function deleteTask(id: number) {
setTasks(tasks.filter((t) => t.id !== id));
}
// Worked out from state on every render. Not stored separately.
const visibleTasks = tasks.filter((t) => (filter === "active" ? !t.done : filter === "done" ? t.done : true));
const left = tasks.filter((t) => !t.done).length;
return (
<main className="app">
<h1>My To-Do List</h1>
<TaskForm onAdd={addTask} />
<FilterBar filter={filter} onChange={setFilter} />
<TaskList tasks={visibleTasks} onToggle={toggleTask} onDelete={deleteTask} />
<p className="count">
{left} {left === 1 ? "task" : "tasks"} left
</p>
</main>
);
}Try it: add tasks, tick them, delete one, and switch between All, Active and Done.
Why the state lives in App: lifting state up
- 1
π
TaskForm needs to add
It calls onAdd(title).
- 2
π
TaskList needs to show
It receives the tasks to draw.
- 3
π’
The counter needs to count
It reads how many aren't done.
- 4
π
So App owns tasks
The closest parent of all three keeps the state and shares it down.
The update patterns, used for real
| Action | Code in App | Array method |
|---|---|---|
| Add | setTasks([...tasks, newTask]) | spread |
| Tick / untick | setTasks(tasks.map((t) => (t.id === id ? { ...t, done: !t.done } : t))) | map |
| Delete | setTasks(tasks.filter((t) => t.id !== id)) | filter |
One problem left: refresh the page and every task disappears, because state only lives in memory. In the next lesson, useEffect saves it.
Key takeaways
- βmap turns an array of data into an array of components.
- βEvery list item needs a stable key so React can track it.
- β&&, ? : and early returns show or hide parts of the UI.
- βShared state lives in the closest common parent: lifting state up.
- βOur To-Do app can now add, tick, delete and filter tasks.
Frequently asked questions
How do I show a list in React?
Use the array's map method to turn each item into JSX: {tasks.map((task) => <TaskItem key={task.id} task={task} />)}. React draws whatever array of elements you return.
Why does React need a key?
The key tells React which item is which between renders, so it can update, add or remove the right row. Use a stable, unique id like task.id. Don't use the array index if items can be removed or reordered.
How do I show something only sometimes?
Use an if with an early return, the && operator ({error && <p>{error}</p>}), or the ternary ? : to choose between two things ({loading ? <Spinner /> : <List />}).
What does 'lifting state up' mean?
When two components need the same data, move the state to their closest common parent and pass it down as props. Our tasks live in App because TaskForm, TaskList and the counter all need them.
Should I store the filtered list in state?
No. If you can calculate something from existing state, calculate it while rendering. Storing it twice means the two copies can disagree.
π§ Quick check: lists and conditions
1. What's the best key for each TaskItem?
2. Which shows the error only when there is one?
3. Where should the visible (filtered) task list come from?
4. Toggle task 5. Which update is correct?
Checking your accessβ¦