Close the earlier lessons and build this from the brief. lib/books.js has
getBooks(), createBook(title) and deleteBook(id).
The brief
A Reading List on one page:
- The books, each with a × button named Remove plus its title for screen readers. With no books: Nothing to read yet.
- A Book title field and an Add book button. While adding, the button says Adding… and is disabled.
- The server checks the title: blank gives Enter a title.; over 40
characters gives Keep the title under 40 characters., keeping what was
typed. Errors are announced with
role="alert". - After a successful add, the field is empty and the error is gone.
Acceptance checks: press Add book with nothing typed; add a long title; add Clean Code; remove both books.
Runs with real Next.js on your computer: unzip, then
npm install and npm run dev. Hint 1: the files
app/actions.js with addBook(previous, formData) and removeBook(id), and a
client app/BookForm.jsx using useActionState.
Hint 2: emptying the field
Return { error: null, title: "" } on success. To make the input take the new
defaultValue, give it key={state.title}: a new key makes React create a
fresh input.
Show one solution
Runs with real Next.js on your computer: unzip, then
npm install and npm run dev. Check yourself
- Where does a server action run, and how does a form call it?
- What does
revalidatePathdo, and what happens without it? - Why does the server validate input even when the browser already does?
Completion is saved in this browser only. It does not measure mastery.