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
mutedclass. - 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.
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
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Check yourself
- Why is
key={index}a problem in a list where items can be removed? - Why doesn't
pushfollowed bysetBooks(books)update the page? - What does
{ ...book, read: true }produce, and why not just setbook.read = true?
Completion is saved in this browser only. It does not measure mastery.