Adding a feature well means following the app's existing shape. In Study
Tasks, that's a pure function in tasks.js for the logic, then a little JSX
that calls it. Do these in the lab below, or in your local project, where you
can add a test for each new function too.
npm install and npm run dev. 1. Clear finished
Add a Clear finished button under the list that removes every done task.
Write the logic as clearFinished(tasks) in tasks.js. Disable the button when
nothing is done.
npm install and npm run dev. Hint 1: the idea
It's a filter that keeps the tasks that aren't done.
Hint 2: a first step
The summary already knows how many are left. Something is finished when
left < tasks.length.
Show one solution
In tasks.js:
export function clearFinished(tasks) {
return tasks.filter((task) => !task.done);
}In App.jsx, import it alongside the others, then add under TaskList:
<button onClick={() => setTasks(clearFinished(tasks))} disabled={left === tasks.length}>
Clear finished
</button>2. Counts on the filters
Show counts on the filter buttons: All (3), To do (2), Done (1). They must update as tasks change.
npm install and npm run dev. Hint 1: the idea
FilterBar has no tasks. Either pass it counts from App, or pass tasks
and work them out there. Passing counts keeps FilterBar simple.
Hint 2: a first step
visibleTasks(tasks, f.value).length gives the count for each filter.
Show one solution
In App.jsx:
const counts = {
all: tasks.length,
active: visibleTasks(tasks, "active").length,
done: visibleTasks(tasks, "done").length,
};<FilterBar filter={filter} onChange={setFilter} counts={counts} />In FilterBar.jsx:
export default function FilterBar({ filter, onChange, counts }) {
return (
<div className="filters" role="group" aria-label="Show">
{filters.map((f) => (
<button key={f.value} aria-pressed={filter === f.value} onClick={() => onChange(f.value)}>
{f.label} ({counts[f.value]})
</button>
))}
</div>
);
}The counts are computed during render, not kept in state, so they can't fall out of step with the tasks.
3. On your own: rename a task
Let the user rename a task: an Edit button opens a labelled text field,
Enter saves, and Escape cancels. A double-click on the title can be a shortcut,
but the feature must also work with the keyboard alone. This one has hints but no solution.
Decide which component owns the "editing" state, write renameTask(tasks, id, title) in tasks.js, and refuse a blank name.
npm install and npm run dev. Hint 1: the idea
Each TaskItem can own its draft and whether it is editing, then call an
onRename callback on save. If you want to enforce only one editor across the
whole list, keep an editingId in the parent instead.
Hint 2: a first step
onKeyDown={(e) => { if (e.key === "Enter") ...; if (e.key === "Escape") ... }}
on the input. Start editing from the Edit button's click handler, and
give the input autoFocus so React focuses it when the editing field first
appears. That lets keyboard users type immediately without learning refs yet.
Completion is saved in this browser only. It does not measure mastery.