Skip to content

On Your Own: Reading Tracker

The Course
•
80 min study

This is the course's final check: a different app, from a brief, with no walkthrough. Plan first. Sketch the components, list the state using Day 1's questions, and decide where each piece lives. Then build one working step at a time. books.js has the starter books and the three statuses.

Expect a first attempt to take 75–90 minutes, and plan to split it over two sittings. Treat that as a guide, not a deadline. If the whole brief is too much for one sitting, first get one complete interaction working, then return for validation and the remaining cases. Before opening the solution, write down what works, what fails, and the next change you would try.

The brief

A Reading Tracker: books you want to read, are reading, and have read.

Data

  • Books live in a reducer with three actions: book_added (new books start as Want to read), book_status_changed and book_removed.
  • The reducer is served by a BooksProvider through two contexts, with useBooks() and useBooksDispatch() hooks that throw a clear error outside the provider.

Screen

  • An add form with a Book title field, labelled with useId. A blank title shows Enter a title., connected with aria-describedby, and focuses the field. After adding, the field is empty and focused again.
  • Filter buttons: All (3), Want to read (1), Reading (1), Read (1). Write the counts in a custom hook, useBookCounts(), and mark the current filter with aria-pressed.
  • Each book shows its title, a status <select> and a × remove button. An empty filter shows No books here yet.
  • An error boundary around the list.

Acceptance checks:

  • Adding Clean Code makes All (4) and Want to read (2), and focus is back in the field.
  • Changing Refactoring UI to Read updates the counts straight away, and under the Want to read filter it disappears from the list.
  • No errors or warnings in the console.
Your workspace React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // Build the Reading Tracker here. See the brief: a reducer behind context, // a custom hook for the counts, an accessible add form, status filters, and // an error boundary around the list. import { starterBooks, statuses } from "./books.js"; export default function App() { return ( <main> <h1>Reading Tracker</h1> </main> ); } export const statuses = [ { id: "want", label: "Want to read" }, { id: "reading", label: "Reading" }, { id: "read", label: "Read" }, ]; export const starterBooks = [ { id: 1, title: "Learning React", status: "reading" }, { id: 2, title: "Eloquent JavaScript", status: "read" }, { id: 3, title: "Refactoring UI", status: "want" }, ]; body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: a plan

Files: state/booksReducer.js, state/BooksContext.jsx, hooks/useBookCounts.js, and components/ for AddBookForm, Filters, BookList and ErrorBoundary. State: the books in the reducer; the current filter in App, since only the filters and the list need it.

Hint 2: start from the Study Board

The reducer, the context file and the boundary have the same shape as the Study Board's. Copy the structure, not the code, and rename as you go.

Show one solution
One solution React · 11 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 AddBookForm from "./components/AddBookForm.jsx"; import BookList from "./components/BookList.jsx"; import ErrorBoundary from "./components/ErrorBoundary.jsx"; import Filters from "./components/Filters.jsx"; import { BooksProvider } from "./state/BooksContext.jsx"; export default function App() { const [filter, setFilter] = useState("all"); return ( <BooksProvider> <main> <h1>Reading Tracker</h1> <AddBookForm /> <Filters filter={filter} onChange={setFilter} /> <ErrorBoundary fallback={<p className="error">The list couldn't be shown.</p>}> <BookList filter={filter} /> </ErrorBoundary> </main> </BooksProvider> ); } export const statuses = [ { id: "want", label: "Want to read" }, { id: "reading", label: "Reading" }, { id: "read", label: "Read" }, ]; export const starterBooks = [ { id: 1, title: "Learning React", status: "reading" }, { id: 2, title: "Eloquent JavaScript", status: "read" }, { id: 3, title: "Refactoring UI", status: "want" }, ]; body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } import { useId, useRef, useState } from "react"; import { useBooksDispatch } from "../state/BooksContext.jsx"; export default function AddBookForm() { const dispatch = useBooksDispatch(); const [title, setTitle] = useState(""); const [error, setError] = useState(""); const inputRef = useRef(null); const id = useId(); function handleSubmit(event) { event.preventDefault(); if (title.trim() === "") { setError("Enter a title."); inputRef.current.focus(); return; } dispatch({ type: "book_added", id: Date.now(), title: title.trim() }); setTitle(""); setError(""); inputRef.current.focus(); } return ( <form className="toolbar" onSubmit={handleSubmit} noValidate> <label htmlFor={id}>Book title</label> <input ref={inputRef} id={id} value={title} onChange={(e) => setTitle(e.target.value)} aria-invalid={error ? true : undefined} aria-describedby={error ? `${id}-error` : undefined} /> <button type="submit">Add book</button> {error && <span id={`${id}-error`} className="error">{error}</span>} </form> ); } import { useBooks, useBooksDispatch } from "../state/BooksContext.jsx"; import { statuses } from "../books.js"; export default function BookList({ filter }) { const books = useBooks(); const dispatch = useBooksDispatch(); const shown = filter === "all" ? books : books.filter((book) => book.status === filter); if (shown.length === 0) return <p className="muted">No books here yet.</p>; return ( <ul className="cards"> {shown.map((book) => ( <li key={book.id} className="card"> <strong>{book.title}</strong>{" "} <select aria-label={`Status of ${book.title}`} value={book.status} onChange={(e) => dispatch({ type: "book_status_changed", id: book.id, status: e.target.value })}> {statuses.map((s) => <option key={s.id} value={s.id}>{s.label}</option>)} </select>{" "} <button onClick={() => dispatch({ type: "book_removed", id: book.id })} aria-label={`Remove ${book.title}`}>×</button> </li> ))} </ul> ); } import { Component } from "react"; // Shows `fallback` instead of its children when rendering them throws. // Error boundaries are still written as classes: there's no hook for this. export default class ErrorBoundary extends Component { state = { error: null }; static getDerivedStateFromError(error) { return { error }; } componentDidCatch(error, info) { console.log("Caught by a boundary:", error.message); } render() { if (this.state.error) return this.props.fallback; return this.props.children; } } import { useBookCounts } from "../hooks/useBookCounts.js"; import { statuses } from "../books.js"; export default function Filters({ filter, onChange }) { const counts = useBookCounts(); const options = [{ id: "all", label: "All" }, ...statuses]; return ( <div className="toolbar" role="group" aria-label="Show"> {options.map((option) => ( <button key={option.id} aria-pressed={filter === option.id} onClick={() => onChange(option.id)}> {option.label} ({counts[option.id]}) </button> ))} </div> ); } import { useBooks } from "../state/BooksContext.jsx"; import { statuses } from "../books.js"; // { all: 3, want: 1, reading: 1, read: 1 }, worked out on every render. export function useBookCounts() { const books = useBooks(); const counts = { all: books.length }; for (const status of statuses) counts[status.id] = books.filter((b) => b.status === status.id).length; return counts; } import { createContext, useContext, useReducer } from "react"; import { booksReducer } from "./booksReducer.js"; import { starterBooks } from "../books.js"; const BooksContext = createContext(null); const BooksDispatchContext = createContext(null); export function BooksProvider({ children }) { const [books, dispatch] = useReducer(booksReducer, starterBooks); return ( <BooksContext value={books}> <BooksDispatchContext value={dispatch}>{children}</BooksDispatchContext> </BooksContext> ); } export function useBooks() { const books = useContext(BooksContext); if (books === null) throw new Error("useBooks must be used inside <BooksProvider>."); return books; } export function useBooksDispatch() { const dispatch = useContext(BooksDispatchContext); if (dispatch === null) throw new Error("useBooksDispatch must be used inside <BooksProvider>."); return dispatch; } export function booksReducer(books, action) { switch (action.type) { case "book_added": return [...books, { id: action.id, title: action.title, status: "want" }]; case "book_status_changed": return books.map((book) => (book.id === action.id ? { ...book, status: action.status } : book)); case "book_removed": return books.filter((book) => book.id !== action.id); default: throw new Error(`Unknown action: ${action.type}`); } }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Your structure can differ. What matters is that state has one home, that every change goes through the reducer, and that the acceptance checks pass.

What next

You can now structure a React app that grows: reducers for complex updates, context for shared state, hooks for reusable logic, and the tools to keep it fast and accessible. From here:

  • Download the Reading Tracker and give it tests, as you did for the Study Board.
  • Read React's guide on react.dev, especially Escape Hatches, which goes deeper into refs and effects.
  • React Advanced in 7 Days picks up from here: component APIs, external stores, Suspense, performance profiling, accessible widgets and testing strategy.
  • For a framework path next, Next.js beginner covers routing, server components and server actions.

Was this article helpful?

Your response is saved on this device.