Skip to content

Study Notes

The Course
•
20 min study

A tour, day by day

Open the files in this order, and find each idea before reading its explanation.

DayIdeaWhere
1Pages from folders; one root layout; a section's own not-found pageapp/, app/layout.jsx, app/notes/[id]/not-found.jsx
2Pages are server components; only the navigation and the form are client componentsapp/NavLinks.jsx, app/new/NoteForm.jsx
3A [id] page reads params; the tag filter lives in searchParams; the current link is marked with usePathnameapp/notes/[id]/page.jsx, app/page.jsx, app/NavLinks.jsx
4The slow stats stream in their own <Suspense>; opening a note shows loading.jsx; error.jsx catches the restapp/page.jsx, app/NoteStats.jsx, app/notes/[id]/loading.jsx, app/error.jsx
5Saving and deleting are server actions, with validation through useActionState, and redirect afterwardsapp/actions.js, app/new/NoteForm.jsx
6The theme cookie; a title template and generateMetadata; a JSON API at /api/notesapp/layout.jsx, app/notes/[id]/page.jsx, app/api/notes/route.js

Three design choices to notice

The form sends back everything it was given. On an error, addNote returns the values as well as the errors, and each field uses them as its defaultValue, so a mistake doesn't discard the submitted draft. Error regions are present with role="alert", and invalid fields are marked with aria-invalid and connected to their messages with aria-describedby:

JavaScript
export async function addNote(previous, formData) {
  const values = {
    title: String(formData.get("title") ?? "").trim(),
    body: String(formData.get("body") ?? "").trim(),
    tag: String(formData.get("tag") ?? ""),
  };
  const errors = {};
  if (values.title === "") errors.title = "Give the note a title.";
  else if (values.title.length > 60) errors.title = "Keep the title under 60 characters.";
  if (values.body === "") errors.body = "Write the note itself.";
  if (!TAGS.includes(values.tag)) values.tag = TAGS[0];
  if (Object.keys(errors).length > 0) return { errors, values };

  const note = await createNote(values);
  revalidatePath("/");
  redirect(`/notes/${note.id}`);
}

Every action ends where the reader expects. Saving goes to the new note; deleting goes to the list. revalidatePath("/") comes first, so the list is fresh when they get there:

JavaScript
export async function removeNote(id) {
  await deleteNote(id);
  revalidatePath("/");
  redirect("/");
}

A key on the stats boundary. Changing the tag is a navigation, and React keeps the old page on screen during a navigation until everything in an already-visible <Suspense> is ready again. Without help, a tag click would seem to do nothing for over a second while the stats were counted. A new key for each tag makes it a new boundary, which shows its fallback at once:

JSX
<Suspense key={tag ?? "all"} fallback={<p className="muted">Counting…</p>}>
Exercise

Follow a request

Without running anything, list in order what happens on the server when the reader presses Delete note on note 2, up to the moment they see the list.

Hint 1: the idea

Trace the form-to-action-to-data-to-navigation path. State which work runs in the browser and which runs on the server.

Hint 2: a first step

Start in notes/[id]/page.jsx at the form, then follow removeNote in actions.js.

Show one solution
  1. The form calls removeNote(2): the id was bound with .bind(null, note.id).
  2. deleteNote(2) removes it from lib/notes.js.
  3. revalidatePath("/") marks the list as out of date.
  4. redirect("/") sends the reader to the list.
  5. The list page runs again: it reads searchParams, loads the notes, and streams the stats into their <Suspense>.

Was this article helpful?

Your response is saved on this device.