Skip to content

The Study Tasks App

The Course
•
20 min study

Today: the whole app

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.

Study Tasks React · 8 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import AddTaskForm from "./components/AddTaskForm.jsx"; import FilterBar from "./components/FilterBar.jsx"; import TaskList from "./components/TaskList.jsx"; import { addTask, removeTask, summary, toggleTask, visibleTasks } from "./tasks.js"; const starterTasks = [ { id: 1, title: "Read about components", minutes: 20, done: true }, { id: 2, title: "Build a counter with useState", minutes: 30, done: false }, { id: 3, title: "Pass props to a child", minutes: 25, done: false }, ]; export default function App() { const [tasks, setTasks] = useState(starterTasks); const [filter, setFilter] = useState("all"); const { left, minutesLeft } = summary(tasks); return ( <main className="app"> <h1>Study Tasks</h1> <p className="summary"> {left === 0 ? "All done!" : `${left} left, ${minutesLeft} minutes`} </p> <AddTaskForm onAdd={(title, minutes) => setTasks(addTask(tasks, title, minutes))} /> <FilterBar filter={filter} onChange={setFilter} /> <TaskList tasks={visibleTasks(tasks, filter)} onToggle={(id) => setTasks(toggleTask(tasks, id))} onRemove={(id) => setTasks(removeTask(tasks, id))} /> </main> ); } // Every change to the task list, as plain functions: each takes the current // tasks and returns a new array. No React here, so they're easy to test. export function addTask(tasks, title, minutes) { return [...tasks, { id: Date.now(), title, minutes, done: false }]; } export function toggleTask(tasks, id) { return tasks.map((task) => (task.id === id ? { ...task, done: !task.done } : task)); } export function removeTask(tasks, id) { return tasks.filter((task) => task.id !== id); } 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), }; } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; background: #f6f8fa; color: #1c2430; } .app { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0; } .summary { color: #087ea4; font-weight: 600; margin-top: 0.25rem; } .task-form { display: flex; flex-wrap: wrap; gap: 0.5rem; } .task-form input { flex: 1 1 10rem; padding: 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; font: inherit; } .task-form input[type="number"] { flex: 0 0 5rem; } button { font: inherit; padding: 0.4rem 0.9rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; } .task-form button { border-color: #087ea4; background: #087ea4; color: white; font-weight: 600; } .error { flex-basis: 100%; margin: 0; color: #b42318; } .filters { display: flex; gap: 0.25rem; margin: 1rem 0 0; } .filters [aria-pressed="true"] { background: #e6f7fc; border-color: #087ea4; color: #06617f; font-weight: 600; } .task-list { list-style: none; padding: 0; } .task { display: flex; align-items: center; gap: 0.5rem; padding: 0.6rem 0; border-bottom: 1px solid #dde3ea; } .task label { flex: 1; } .task.done label { text-decoration: line-through; color: #6b7785; } .minutes { color: #6b7785; font-variant-numeric: tabular-nums; } .remove { border: 0; background: none; color: #6b7785; font-size: 1.2rem; padding: 0 0.4rem; } .empty { color: #6b7785; } import { useState } from "react"; export default function AddTaskForm({ onAdd }) { const [title, setTitle] = useState(""); const [minutes, setMinutes] = useState("15"); const [error, setError] = useState(""); function handleSubmit(event) { event.preventDefault(); const amount = Number(minutes); if (title.trim() === "") { setError("Give the task a name."); return; } if (!Number.isInteger(amount) || amount <= 0) { setError("Minutes must be a whole number above 0."); return; } onAdd(title.trim(), amount); setTitle(""); setError(""); } return ( <form className="task-form" onSubmit={handleSubmit}> <input aria-label="Task" placeholder="What will you study?" value={title} onChange={(event) => setTitle(event.target.value)} /> <input aria-label="Minutes" type="number" value={minutes} onChange={(event) => setMinutes(event.target.value)} /> <button type="submit">Add</button> {error && <p className="error">{error}</p>} </form> ); } const filters = [ { value: "all", label: "All" }, { value: "active", label: "To do" }, { value: "done", label: "Done" }, ]; export default function FilterBar({ filter, onChange }) { 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} </button> ))} </div> ); } export default function TaskItem({ task, onToggle, onRemove }) { return ( <li className={task.done ? "task done" : "task"}> <label> <input type="checkbox" checked={task.done} onChange={() => onToggle(task.id)} />{" "} {task.title} </label> <span className="minutes">{task.minutes} min</span> <button className="remove" onClick={() => onRemove(task.id)} aria-label={`Remove ${task.title}`}> × </button> </li> ); } import TaskItem from "./TaskItem.jsx"; export default function TaskList({ tasks, onToggle, onRemove }) { if (tasks.length === 0) { return <p className="empty">Nothing here.</p>; } return ( <ul className="task-list"> {tasks.map((task) => ( <TaskItem key={task.id} task={task} onToggle={onToggle} onRemove={onRemove} /> ))} </ul> ); }
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.

DayIdeaWhere
1Components in their own files, imported and exportedApp.jsx and components/
2Props, including callback props like onToggleTaskList and TaskItem
2Conditional renderingthe summary in App, the empty state in TaskList, the error in AddTaskForm
3State and eventstasks and filter in App; the form fields in AddTaskForm
4Lists with keys; new arrays instead of changing old onesTaskList, and every function in tasks.js
5A controlled, validated form; state lifted to AppAddTaskForm

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:

JavaScript
onToggle={(id) => setTasks(toggleTask(tasks, id))}

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

  1. You tick Pass props to a child while the To do filter is on. What happens to it, and why?
  2. FilterBar doesn't have any state. How does it know which button to highlight?
Check your predictions
  1. 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.
  2. 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.

Was this article helpful?

Your response is saved on this device.