Sub topic 5
Showing pending UI with useFormStatus
This chapter assumes you know React function components and hooks, the difference between controlled and uncontrolled inputs, and how to pass a function to a form's action prop. You should also know that React runs a form action inside a transition. Earlier chapters covered those topics.
The problem useFormStatus solves
When a form's action is async, the user needs feedback. The submit button should be disabled so a second click does not send a duplicate request. A label such as "Saving..." tells the user that something is happening.
You could track this yourself with useState, or with the isPending flag from useActionState. That works, but it has a cost. The component that owns the form must pass the pending flag down to every child that needs it. In a design system, your SubmitButton often lives in a shared file and knows nothing about which form it sits in.
useFormStatus removes that wiring. A component rendered inside a <form> can ask React for the status of its parent form, much like reading a context value.
import { useFormStatus } from "react-dom";Note the import source. useFormStatus comes from react-dom, not react. This is easy to get wrong, because the other hooks in this tutorial (useTransition, useActionState, useOptimistic) come from react.
The basic pattern
Here is the smallest useful example. It is a button that disables itself and changes its label while the parent form's action runs.
import { useFormStatus } from "react-dom";
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Saving..." : "Save"}
</button>
);
}
export function ProfileForm({ saveProfile }) {
return (
<form action={saveProfile}>
<input name="displayName" />
<SubmitButton />
</form>
);
}ProfileForm renders the form and passes saveProfile as its action. SubmitButton is a separate component rendered as a child of the form. When the user submits, React starts the action. pending becomes true in SubmitButton, and it goes back to false when the action finishes.
ProfileForm does not hold any pending state and does not pass a prop to the button. The button finds the form on its own.
What the hook returns
useFormStatus takes no arguments. It returns an object with four properties.
| Property | Type | Meaning |
|---|---|---|
| `pending` | boolean | `true` while the parent form's action is running |
| `data` | `FormData` or `null` | The values being submitted, or `null` when nothing is pending |
| `method` | string | The HTTP method of the form, `"get"` or `"post"`. The default is `"get"` |
| `action` | function, string, or `null` | The value of the parent form's `action` prop |
Most code uses only pending. The data property is useful when the button or a nearby component wants to describe what is being sent.
import { useFormStatus } from "react-dom";
function SavingNotice() {
const { pending, data } = useFormStatus();
if (!pending) return null;
const name = data.get("displayName");
return <p>Saving the name "{name}"...</p>;
}When pending is false, data is null. Reading data.get(...) without first checking pending throws an error, so keep the early return.
The rule that causes most bugs
useFormStatus reads the status of a form that is a parent of the component calling it. It does not read the status of a form that the same component renders.
This version looks reasonable and never works:
import { useFormStatus } from "react-dom";
export function BrokenForm({ saveProfile }) {
// Wrong: this component renders the form, so it is not inside one.
const { pending } = useFormStatus();
return (
<form action={saveProfile}>
<input name="displayName" />
<button type="submit" disabled={pending}>
Save
</button>
</form>
);
}Here pending is always false. At the point where the hook runs, BrokenForm has no parent <form>. The form it renders is its child, and the hook does not look downward.
The fix is the one you saw earlier. Move the hook into a child component and render that child inside the form.
flowchart TDThe diagram below shows the lookup direction. Only components below the form can read its status.
Which form does it read?
useFormStatus reports on the nearest parent <form>. It does not take a form ID or a ref, so you cannot point it at a different form. If a page has two forms, each SubmitButton instance reads the form it sits in.
import { useFormStatus } from "react-dom";
function SubmitButton({ children }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{children}
</button>
);
}
export function Settings({ saveName, saveEmail }) {
return (
<div>
<form action={saveName}>
<input name="name" />
<SubmitButton>Save name</SubmitButton>
</form>
<form action={saveEmail}>
<input name="email" type="email" />
<SubmitButton>Save email</SubmitButton>
</form>
</div>
);
}Submitting the first form disables only the first button. The two instances of SubmitButton do not affect each other. This is why a single shared button component works across your whole app.
When the status is pending
pending is true only for forms whose action prop is a function. React tracks the status because it runs your function inside a transition, and the transition is what stays pending until the returned promise settles.
If the action is a URL string, the browser performs a normal navigation or page load, and there is no React transition to track. The same is true when a form has no action and the page uses onSubmit with event.preventDefault(). In that case useFormStatus does not see a submission, because React did not start the action. Move the logic to a function passed to action, or track the state yourself.
Also keep in mind that pending follows the lifetime of the Action. It ends when the async function finishes, including any await calls inside it. If your action returns before a slow background task completes, the button re-enables at that point.
import { useFormStatus } from "react-dom";
function SubmitButton() {
const { pending } = useFormStatus();
return <button disabled={pending}>Send</button>;
}
export function SlowForm() {
async function send(formData) {
// pending stays true during this entire await.
await new Promise((resolve) => setTimeout(resolve, 2000));
console.log("Sent", formData.get("note"));
}
return (
<form action={send}>
<input name="note" />
<SubmitButton />
</form>
);
}For the two seconds that send waits, the button is disabled. Then pending returns to false and the button works again.
Beyond the submit button
A pending form often needs more than a disabled button. Because any descendant component can call the hook, you can build small reusable pieces.
A fieldset wrapper can disable every input while the form is pending:
import { useFormStatus } from "react-dom";
function FormFields({ children }) {
const { pending } = useFormStatus();
return <fieldset disabled={pending}>{children}</fieldset>;
}
export function ContactForm({ sendMessage }) {
return (
<form action={sendMessage}>
<FormFields>
<input name="email" type="email" />
<textarea name="message" />
<button type="submit">Send</button>
</FormFields>
</form>
);
}There is one trap here. Disabled inputs are excluded from FormData. That is only a problem if the fieldset becomes disabled before React reads the values. React builds the FormData object at the moment of submission, before pending becomes true, so the values are captured. Disabling the fields afterward is safe. If you instead disabled the fields by some other path before the submit event fired, those values would be missing.
You can also show a spinner in a shared component, or lower the opacity of a section of the page. The pattern is the same: call the hook in a child of the form and render something based on pending.
Accessibility notes
Disabling a button when pending is true stops duplicate submissions. It also removes the button from the keyboard focus order, which can confuse some screen reader users, because focus may jump elsewhere. Two options help:
- Use
aria-disabled={pending}instead ofdisabled, and ignore clicks in the action. This keeps focus on the button. - Add a status message with
role="status"so assistive technology announces that saving is in progress.
import { useFormStatus } from "react-dom";
function StatusMessage() {
const { pending } = useFormStatus();
return (
<p role="status">{pending ? "Saving your changes..." : ""}</p>
);
}Keep the role="status" element in the DOM at all times and change only its text. Live regions announce changes to elements that already exist, so an element that appears at the same moment as its text is often not announced.
useFormStatus compared with the alternatives
You now have three ways to learn that a mutation is running. They overlap, so it helps to know when each fits.
| Approach | Where it works | Best used when |
|---|---|---|
| `useFormStatus` | In a child of a `<form>` | A shared component, such as a submit button, needs the status without props |
| `isPending` from `useActionState` | In the component that owns the action | The same component that renders the form also shows the pending state |
| `isPending` from `useTransition` | In the component that calls `startTransition` | You run an Action from an event handler rather than a form |
useFormStatus does not give you the action's result or errors. For that, use useActionState, which the next chapters cover. The two hooks work together: useActionState returns the result, and useFormStatus lets deeper components react to the pending state.
Common errors
- Calling the hook in the form's own component.
pendingstaysfalse. Move the hook into a child rendered inside the<form>. - Importing from
react.useFormStatusis exported byreact-dom. Importing it fromreactleaves you withundefined, and calling it throws an error. - Reading
datawhile idle.dataisnullwhen nothing is pending. Checkpendingfirst. - Using a string action or
onSubmit. React only tracks function actions, sopendingnever turns on. - Expecting it to work across forms. The hook reads only the nearest parent form.
Exercise
A teammate writes a component named CommentForm. It calls useFormStatus() at the top, then returns a <form action={postComment}> containing a button with disabled={pending}. The button never disables during submission. Explain why, and show the fix.
Show answer
useFormStatus reads the status of a parent form of the component that calls it. CommentForm renders the form, so at the point of the call it has no parent form, and pending is always false. Fix it by moving the hook into a child component, for example function SubmitButton() { const { pending } = useFormStatus(); return <button type="submit" disabled={pending}>Post</button>; }, and rendering <SubmitButton /> inside the <form> in CommentForm.
Exercise
A form uses <form onSubmit={handleSubmit}> with event.preventDefault() and an async fetch inside the handler. A child button calls useFormStatus() and never shows pending as true. What are two ways to get a working pending state?
Show answer
React tracks pending status only when the form's action prop is a function, because that is what starts an Action. Option one: move the logic into a function and pass it as action, such as <form action={submit}>, reading values from FormData. Option two: keep the onSubmit handler and track the state yourself, for example by wrapping the work in startTransition and using isPending from useTransition, or with a useState flag.