Skip to content

Checkpoint: A Reading List

The Course
•
20 min study

Close the earlier lessons and build this from the brief. The starter already holds the state.

The brief

A reading list. Each book has an id, a title and a read flag.

  • Show each book in a list item, with a Mark read button (Mark unread for a book already read) and a Remove button. Show read titles in grey, with the muted class.
  • Above the list, show a summary such as 1 of 2 read.
  • When the list is empty, show Your list is empty. instead of the summary and the list.
  • An Add a book button adds the next title from moreBooks, unread. Disable it once all three have been added.

Acceptance checks:

  • Mark read on Eloquent JavaScript changes the summary to 2 of 2 read.
  • Removing both books shows Your list is empty.
  • Adding a book after a removal works, and every book keeps its own read state.
  • No warnings in the console, including the key warning.
Your workspace React · 3 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"; const moreBooks = ["Thinking in React", "Learning JavaScript", "Clean Code"]; export default function App() { const [books, setBooks] = useState([ { id: 1, title: "The Pragmatic Programmer", read: true }, { id: 2, title: "Eloquent JavaScript", read: false }, ]); return ( <main> <h1>Reading list</h1> {/* Show the books, the summary and the buttons here. */} </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: which method for each button?

Add: spread into a new array. Remove: filter. Mark read: map, with a new object for the changed book.

Hint 2: knowing what to add next

Keep a count of how many books you've added in a second piece of state. It's both the index into moreBooks and the reason to disable the button. For the new book's id, the starter ids are 1 and 2, so 3 + added gives each of these three additions a different id. Removing a book must not reset added.

Show one solution
One solution React · 4 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 BookRow from "./BookRow.jsx"; const moreBooks = ["Thinking in React", "Learning JavaScript", "Clean Code"]; export default function App() { const [books, setBooks] = useState([ { id: 1, title: "The Pragmatic Programmer", read: true }, { id: 2, title: "Eloquent JavaScript", read: false }, ]); const [added, setAdded] = useState(0); const readCount = books.filter((book) => book.read).length; function addBook() { const title = moreBooks[added]; setBooks([...books, { id: 3 + added, title, read: false }]); setAdded(added + 1); } function toggleRead(id) { setBooks(books.map((book) => (book.id === id ? { ...book, read: !book.read } : book))); } function removeBook(id) { setBooks(books.filter((book) => book.id !== id)); } return ( <main> <h1>Reading list</h1> {books.length === 0 ? ( <p>Your list is empty.</p> ) : ( <> <p>{readCount} of {books.length} read</p> <ul> {books.map((book) => ( <BookRow key={book.id} book={book} onToggle={toggleRead} onRemove={removeBook} /> ))} </ul> </> )} <button onClick={addBook} disabled={added >= moreBooks.length}>Add a book</button> </main> ); } export default function BookRow({ book, onToggle, onRemove }) { return ( <li> <span className={book.read ? "muted" : ""}>{book.title}</span>{" "} <button onClick={() => onToggle(book.id)}>{book.read ? "Mark unread" : "Mark read"}</button>{" "} <button onClick={() => onRemove(book.id)} aria-label={`Remove ${book.title}`}>Remove</button> </li> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Check yourself

  1. Why is key={index} a problem in a list where items can be removed?
  2. Why doesn't push followed by setBooks(books) update the page?
  3. What does { ...book, read: true } produce, and why not just set book.read = true?

Was this article helpful?

Your response is saved on this device.