Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 12 / Topic 4 of 7

useFormStatus — must be its own component, nested inside the <form>

// harness/app/swiggy-menu/reviews/ReviewForm.tsx
function SubmitButton() {
  const { pending } = useFormStatus();
  return <button type="submit" disabled={pending}>{pending ? "Posting…" : "Post review"}</button>;
}

export function ReviewForm() {
  const [state, formAction] = useActionState(submitReview, initialReviewState);
  return (
    <form action={formAction}>
      {/* ...fields... */}
      <SubmitButton />   {/* rendered INSIDE the form, as its own component */}
    </form>
  );
}

SubmitButton has to be a separate component, nested as a child of the <form> — not inlined into ReviewForm itself. The installed forms.md guide's own wording explains why: "you'll need to create a separate component to render the loading indicator" when using useFormStatus, because the hook only reports the status of the nearest parent <form>, relative to where it's called — calling it in the same component that renders the <form> element doesn't give it a parent form to report on yet, at the point that component itself is evaluated. ReviewForm already has pending available from useActionState's own third return value (and uses it too, for the "Submitting your review…" line above the fields) — useFormStatus isn't the only way to read this state, it's the way that's scoped specifically for a component nested inside the form, independent of whatever rendered the form itself.

Done here? The practice set for Part 12 — including the solutions — is on the part page.

Search the guide