useFormStatus() tells a component whether the form it's inside is being
submitted. While the action runs, pending is true: the button says
Adding… and is disabled, so it can't be pressed twice.
It's a hook, so it needs a client component. The page and the form stay server
code; only the button is a client component.
Predict
Move <SubmitButton /> out of the form, to just after </form>, and put a
plain <button>Add</button> inside the form instead. Submit a note. Does the
moved SubmitButton ever say Adding…?
Check your prediction
No. useFormStatus only reports on the form that contains the component.
Outside any form, it's never pending. Undo the change.
Exercise
Any labels
Make SubmitButton reusable: give it label and pendingLabel props, and use
it here as Add note / Saving note….
Your working spaceNext.js · 6 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>Study Notes</header>
{children}
</body>
</html>
);
}
import { getNotes } from "@/lib/notes.js";
import { addNote } from "./actions.js";
import SubmitButton from "./SubmitButton.jsx";
// Read the notes fresh on every request.
export const dynamic = "force-dynamic";
export default async function HomePage() {
const notes = await getNotes();
return (
<main>
<h1>Notes</h1>
<ul>
{notes.map((note) => <li key={note.id}>{note.text}</li>)}
</ul>
<form action={addNote}>
<input name="text" aria-label="New note" />
<SubmitButton />
</form>
</main>
);
}
"use server";
import { revalidatePath } from "next/cache";
import { createNote } from "@/lib/notes.js";
// A server action: a function the browser can call, that runs on the server.
export async function addNote(formData) {
const text = formData.get("text");
console.log("Adding note:", text);
await createNote(text);
revalidatePath("/");
}
"use client";
import { useFormStatus } from "react-dom";
// Must be inside a <form>: useFormStatus reports on the form around it.
export default function SubmitButton() {
const { pending } = useFormStatus();
return (
<button disabled={pending}>
{pending ? "Adding…" : "Add"}
</button>
);
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
// A pretend database, kept in the server's memory. A real app would use a
// database; memory is emptied whenever the server restarts (here, every Run).
const notes = [{ id: 1, text: "Read about server actions", done: false }];
let nextId = 2;
const wait = () => new Promise((resolve) => setTimeout(resolve, 400));
export async function getNotes() {
return notes;
}
export async function createNote(text) {
await wait();
notes.push({ id: nextId++, text, done: false });
}
export async function deleteNote(id) {
await wait();
const index = notes.findIndex((note) => note.id === id);
if (index !== -1) notes.splice(index, 1);
}
export async function toggleNote(id) {
await wait();
const note = notes.find((note) => note.id === id);
if (note) note.done = !note.done;
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
Reusable components receive their varying content as props while keeping the same pending-state behaviour.
Hint 2: a first step
Replace the two strings in the button with the props.