Today brings the week together in one app. You'll read it, run it on your own
computer, test it, and extend it. Then you'll build a different app from a
brief, on your own.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Use the app first: add a task, tick it, remove it, and try the three filters.
Try adding a task with no name, and with 0 minutes.
A tour, day by day
Open each file and find these. Explain each one to yourself before moving on.
Day
Idea
Where
1
Components in their own files, imported and exported
App.jsx and components/
2
Props, including callback props like onToggle
TaskList and TaskItem
2
Conditional rendering
the summary in App, the empty state in TaskList, the error in AddTaskForm
3
State and events
tasks and filter in App; the form fields in AddTaskForm
4
Lists with keys; new arrays instead of changing old ones
TaskList, and every function in tasks.js
5
A controlled, validated form; state lifted to App
AddTaskForm
There's no effect yet. You'll add one in the next lesson.
One new idea: tasks.js
On Day 4, the code that built new arrays lived inside App's handlers. Here
it has moved to src/tasks.js, as plain functions:
JavaScript
export function toggleTask(tasks, id) { return tasks.map((task) => (task.id === id ? { ...task, done: !task.done } : task));}
Each function takes the current tasks and returns new ones. It doesn't import
React, doesn't call setTasks, and doesn't touch the page. App connects them
to state:
This split pays off in two ways. App reads as a list of what can happen,
without the details of how. And because the functions are plain JavaScript,
they're easy to test, which you'll do in the Project Tests lesson.
summary and visibleTasks work the same way, but they compute values for
rendering instead of building new state. filter is state; the visible tasks
are not, because they can be worked out from tasks and filter:
JavaScript
export function visibleTasks(tasks, filter) { if (filter === "active") return tasks.filter((task) => !task.done); if (filter === "done") return tasks.filter((task) => task.done); return tasks;}export function summary(tasks) { const left = tasks.filter((task) => !task.done); return { left: left.length, minutesLeft: left.reduce((sum, task) => sum + task.minutes, 0), };}
Predict
You tick Pass props to a child while the To do filter is on. What
happens to it, and why?
FilterBar doesn't have any state. How does it know which button to
highlight?
Check your predictions
It disappears from the list, because visibleTasks(tasks, "active") no
longer includes it. The summary changes at the same moment, because both are
worked out from the same tasks.
App passes filter down as a prop, and each button sets
aria-pressed={filter === f.value}. The CSS styles [aria-pressed="true"].
Screen readers announce the pressed button too.
Completion is saved in this browser only. It does not measure mastery.