Skip to content

Showing Progress

The Course
•
15 min study

Saving takes a moment. Without a sign that it's happening, people press the button again, and the note is saved twice.

A submit button that knows Next.js · 6 files
import "./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.
Add and remove files here; write their code in the editor.

Add a note and watch the button.

useFormStatus

JSX
"use client";

import { useFormStatus } from "react-dom";

export default function SubmitButton() {
  const { pending } = useFormStatus();
  return <button disabled={pending}>{pending ? "Adding…" : "Add"}</button>;
}

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 space Next.js · 6 files
import "./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.
Add and remove files here; write their code in the editor.
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.

Show one solution
JSX
export default function SubmitButton({ label, pendingLabel }) {
  const { pending } = useFormStatus();
  return <button disabled={pending}>{pending ? pendingLabel : label}</button>;
}
JSX
<SubmitButton label="Add note" pendingLabel="Saving note…" />

Was this article helpful?

Your response is saved on this device.