Sub topic 5
Showing pending UI with useFormStatus
This chapter assumes you can write React function components with hooks, know how a form's action prop accepts a function (covered in the previous chapter), and are comfortable with async functions and FormData.
The problem it solves
When a form's action is running, users need feedback. They should see that the submit worked, and they should not be able to click the button a second time. With useTransition you get an isPending flag, but only in the component that called the hook. A form action passed straight to <form action> has no such hook call in your component. You still need a way to read "is this form submitting?"
useFormStatus is a hook that reads the status of the parent <form>. It is exported from react-dom, not from react.
A basic example
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. SubmitButton is a separate component rendered inside it. While saveProfile runs, pending is true, the button is disabled, and its label changes. When the action finishes, pending goes back to false.
What the hook returns
useFormStatus returns an object. The most-used field is pending.
| Field | Meaning |
|---|---|
| `pending` | `true` while the parent form's action is running |
| `data` | The `FormData` being submitted, or `null` when idle |
| `method` | The form's HTTP method, `"get"` or `"post"` |
| `action` | The function or URL passed to the form's `action` prop |
The data field is useful for showing what is being sent. For example, you can display "Saving Ada..." by reading data.get("displayName").
import { useFormStatus } from "react-dom";
function StatusLine() {
const { pending, data } = useFormStatus();
if (!pending) return null;
return <p>Saving {String(data?.get("displayName") ?? "")}...</p>;
}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 look at forms rendered by that same component.
import { useFormStatus } from "react-dom";
// Wrong: this component renders the form itself.
function BrokenForm({ saveProfile }) {
const { pending } = useFormStatus(); // always false
return (
<form action={saveProfile}>
<input name="displayName" />
<button type="submit" disabled={pending}>
Save
</button>
</form>
);
}In BrokenForm, there is no parent form above the component, so pending never becomes true. The button never disables. The fix is the pattern from the basic example: move the button into its own child component and render that inside the <form>.
How the data flows
Useful patterns
Because any descendant can read the status, you can build small reusable pieces and drop them into any form.
- A shared
SubmitButtonused across your app, so every form disables and relabels the same way. - A spinner or progress line placed anywhere inside the form.
- Fieldsets that disable inputs while submitting, using
pendingas thedisabledvalue.
Keep in mind these limits:
- It reads only the nearest parent form. It cannot report on a form elsewhere on the page.
- It reports status only. It does not give you the action's return value or errors. Use
useActionStatefor that, which is covered in the next chapter. - It tracks forms whose
actionprop is a function. Disabling the button is a UI convenience, not a guard against duplicate requests on the server.
Exercise
A developer calls useFormStatus() at the top of a CheckoutForm component and uses pending to disable the submit button inside the same component's returned <form>. The button never disables during submission. What is wrong, and how do you fix it?
Show answer
useFormStatus reads the status of a parent form, and CheckoutForm renders the form itself, so there is no parent form above the hook call and pending stays false. Move the button into a child component such as SubmitButton, call useFormStatus there, and render <SubmitButton /> inside the <form>.
Exercise
Which field of the useFormStatus result would you read to display the value the user typed into an input named displayName while the form is submitting, and how?
Show answer
Read data, which holds the submitted FormData while pending. Call data.get("displayName"), and guard against null when the form is idle, for example data?.get("displayName").