Sub topic 9
Reading status in child components with useFormStatus
This chapter assumes you know React function components and useState, have used useActionState with a form action, and understand that React 19 runs form actions inside transitions. You should also be comfortable reading FormData in an async function.
The problem: passing isPending down
useActionState returns isPending as its third value. That works when the button lives in the same component as the hook. In real apps, the submit button is often a shared component used in many forms. Passing isPending into it as a prop works, but every form must wire it up by hand, and each new form can forget to.
// Works, but every form must remember to pass isPending.
function SubmitButton({ pending, children }) {
return (
<button type="submit" disabled={pending}>
{pending ? "Saving..." : children}
</button>
);
}useFormStatus removes that wiring. A component calls the hook and learns whether the nearest parent form is submitting.
What useFormStatus returns
useFormStatus is exported from react-dom, not from react. It takes no arguments and returns an object:
| Property | Meaning |
|---|---|
| `pending` | `true` while the parent form's action is running |
| `data` | The `FormData` being submitted, or `null` when idle |
| `method` | `"get"` or `"post"`, from the form's method |
| `action` | The function or URL passed to the form's `action` prop |
Most code only reads pending. The other fields are useful for showing what is being submitted, for example "Saving title..." in a status line.
The basic pattern
import { useFormStatus } from "react-dom";
function SubmitButton({ children }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Saving..." : children}
</button>
);
}Use it inside a form that is driven by useActionState:
import { useActionState } from "react";
import { useFormStatus } from "react-dom";
async function saveTitle(prevState, formData) {
const title = String(formData.get("title") ?? "").trim();
if (!title) {
return { error: "Title is required.", title };
}
await new Promise((resolve) => setTimeout(resolve, 800));
return { error: null, title: "" };
}
function SubmitButton({ children }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Saving..." : children}
</button>
);
}
export function TitleForm() {
const [state, formAction] = useActionState(saveTitle, {
error: null,
title: "",
});
return (
<form action={formAction}>
<input name="title" type="text" defaultValue={state.title} />
<SubmitButton>Save</SubmitButton>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}TitleForm no longer destructures isPending. SubmitButton finds out on its own. The same button can now go into any form.
How the hook finds its form
The hook reads status from context-like information supplied by the nearest ancestor <form>. That explains the rule that trips up most people.
The hook reports on a <form> that is a parent of the calling component. It does not look at a form rendered by the same component. If you call it in TitleForm, the form is a child, not a parent, so pending is always false.
import { useFormStatus } from "react-dom";
// Wrong: the form is rendered here, so it is not an ancestor.
export function BrokenForm({ action }) {
const { pending } = useFormStatus(); // always false
return (
<form action={action}>
<button type="submit" disabled={pending}>Save</button>
</form>
);
}The fix is to move the hook call into a component that the form renders, as in SubmitButton above.
Which form does it report on?
The hook reports on the closest parent form only. With nested components, any depth works, as long as a <form> sits above in the tree. Two sibling forms on one page each report their own status, because each button reads from its own ancestor.
HTML does not allow a <form> inside another <form>, so you will not normally have to reason about nested forms.
Beyond the button
A status component does not have to be a button. Any descendant of the form can use the hook. Common uses:
- Disabling or dimming a group of inputs while saving.
- Showing a spinner or status line.
- Displaying the submitted values using
data.
import { useFormStatus } from "react-dom";
function FormStatusLine() {
const { pending, data } = useFormStatus();
if (!pending) return null;
return <p aria-live="polite">Saving "{data?.get("title")}"...</p>;
}
function Fieldset({ children }) {
const { pending } = useFormStatus();
return <fieldset disabled={pending}>{children}</fieldset>;
}Here FormStatusLine reads the title the user just submitted while the action runs. Note that Fieldset calls the hook itself, so it must be rendered inside the form. It cannot wrap the form.
A disabled <fieldset> disables every control inside it. That prevents edits during submission. A disabled control is also left out of the submitted data, but React has already captured the FormData by the time pending becomes true, so this does not affect the current submission.
useFormStatus compared with isPending
Both report the same event, a form action running in a transition. They differ in where they are available.
| Row | `isPending` from `useActionState` | `useFormStatus` |
|---|---|---|
| Where it works | The component that calls `useActionState` | Any component rendered inside the form |
| Needs a prop to reach children | Yes | No |
| Works without `useActionState` | No | Yes, with any function passed to `action` |
| Covers calls outside a form | Yes, via `startTransition(() => dispatch(...))` | No, only form submissions |
| Gives the submitted `FormData` | No | Yes, through `data` |
The last two rows matter. useFormStatus is tied to a <form> submission. If you call the dispatcher from a button's onClick as covered in a later chapter, the pending state shows up in isPending but not in useFormStatus, because no form was submitted.
useFormStatus also does not need useActionState. Any function passed to <form action={...}> triggers it, including a plain async function.
Prefer useFormStatus for reusable pieces such as submit buttons and spinners. Use isPending when the component that owns the form needs the flag itself, for example to disable something outside the form.
Edge cases and gotchas
- Wrong import.
useFormStatuscomes fromreact-dom. Importing it fromreactgivesundefined, and calling it throws aTypeError. - Same-component call. As shown above,
pendingstaysfalsewhen the form is rendered by the component that calls the hook. This fails silently, so check it first if a button never disables. - Disabling the button early. While
pendingistrue, a disabled submit button blocks repeat clicks. If you do not disable it, repeat submissions are queued and run in order, which the queueing chapter covers. - Status is per submission cycle.
pendingreturns tofalsewhen the action finishes. After that,dataisnullagain. Do not store values fromdataif you need them later. Keep them in the action's returned state. - Not a place for results. The hook says whether something is running. It does not tell you what the action returned. Success and error results belong in the
useActionStatestate.
A reusable form kit
Putting the pieces together, a small set of status-aware components keeps each form short:
import { useActionState } from "react";
import { useFormStatus } from "react-dom";
function SubmitButton({ children, pendingText = "Working..." }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending} aria-disabled={pending}>
{pending ? pendingText : children}
</button>
);
}
async function rename(prevState, formData) {
const name = String(formData.get("name") ?? "").trim();
if (name.length < 3) {
return { error: "Use at least 3 characters.", name };
}
await new Promise((resolve) => setTimeout(resolve, 500));
return { error: null, name };
}
export function RenameForm() {
const [state, formAction] = useActionState(rename, { error: null, name: "" });
return (
<form action={formAction}>
<input name="name" type="text" defaultValue={state.name} />
<SubmitButton pendingText="Renaming...">Rename</SubmitButton>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}The form owns the state and the action. The button owns only how pending looks. Each concern stays in one place.
Exercise
A developer writes const { pending } = useFormStatus(); at the top of ContactForm, then renders <form action={formAction}> with a submit button that uses disabled={pending}. The button never disables during submission. Why, and how do you fix it?
Show answer
useFormStatus reports on a <form> that is an ancestor of the calling component. In ContactForm the form is rendered by the component itself, so there is no parent form and pending is always false. Move the hook call into a child component, such as a SubmitButton, that is rendered inside the <form>, and render that component in place of the raw button.
Exercise
You call dispatch(1) from a button's onClick inside startTransition, and the button is not inside any form. Will useFormStatus report pending: true while the action runs? What do you use instead?
Show answer
No. useFormStatus only reflects a form submission. Use the isPending value returned by useActionState, which covers dispatcher calls made outside a form.