A delete button needs to say which note. Its action needs the note's id.
npm install and npm run dev. Add two notes, then delete one with ×. Look at the console.
bind
<form action={removeNote.bind(null, note.id)}>
<button aria-label={`Delete ${note.text}`}>×</button>
</form>export async function removeNote(id) {
await deleteNote(id);
revalidatePath("/");
}removeNote.bind(null, note.id) makes a new function with the id filled in as
its first argument. Each note's form gets its own, so pressing a button calls
removeNote(2) or removeNote(3). The null is a detail of bind; always pass
null here.
Each button is a tiny form. That's normal in Next.js: a form is how the browser calls a server action, and it works even before the page's JavaScript has loaded.
The × alone means nothing to a screen reader, so each button has an
aria-label naming its note.
Why not onClick?
The page is a server component, so it can't use onClick, as Day 2 showed. You
could write a client component that calls removeNote(note.id) from a click
handler; that works too. A form keeps the page on the server and needs no client
code at all.
Mark done
Add a ✓ button before each × that marks a note done, drawing a line
through it. Pressing it again (now ↺) marks it not done. lib/notes.js
already has toggleNote(id).
npm install and npm run dev. Hint 1: the idea
Marking a note done changes server data, just like deleting one. So it's the same shape: a Server Action changes the data and refreshes the page, and each note's form tells the action which note it is.
Hint 2: a first step
Write a toggleDone(id) action next to removeNote, and give each note a
second form bound to its id, like the × form, with a ✓ button. Press it
twice and check note.done flips. Then draw the line through the text, switch
the symbol, and give the button a name that says what it will do.
Show one solution
In actions.js:
import { createNote, deleteNote, toggleNote } from "@/lib/notes.js";
export async function toggleDone(id) {
await toggleNote(id);
revalidatePath("/");
}In page.jsx, import toggleDone, and in each item:
<span style={{ textDecoration: note.done ? "line-through" : "none" }}>{note.text}</span>{" "}
<form action={toggleDone.bind(null, note.id)} style={{ display: "inline" }}>
<button aria-label={`Mark ${note.text} ${note.done ? "not done" : "done"}`}>{note.done ? "↺" : "✓"}</button>
</form>{" "}bind(null, note.id) fills in the action's first argument, so each form
changes its own note without a hidden input. revalidatePath("/") makes the
server render the list again with the new done. The aria-label names what
the button will do next, because ✓ and ↺ alone say nothing to a screen
reader.
Completion is saved in this browser only. It does not measure mastery.