Sub topic 2
What useFormStatus does and where it comes from
This chapter assumes you know React function components, hooks, and props. It also assumes you have used HTML forms and FormData, and that you are comfortable with async functions. Here you learn what useFormStatus is, where to import it from, and what it returns. Later chapters cover the child-component rule, a reusable button, and the pitfalls in more detail.
The problem it solves
When a form submits an async action, users need feedback. They should see that something is happening, and they should not be able to click the button twice. Before form actions, you usually tracked this with your own useState flag, set it before the request, and cleared it afterward.
useFormStatus removes that bookkeeping for a common case. It lets a component read the status of the form it sits in, without any state or props passed down from the form's owner.
Where it comes from
useFormStatus is not exported from react. It comes from react-dom, because it is tied to the DOM <form> element.
import { useFormStatus } from 'react-dom';This is a common source of confusion. The related hook useActionState is imported from react, while useFormStatus is imported from react-dom. You need a React version that supports form actions (React 19 or later, or a framework release that ships it).
How it connects to form actions
A form action is a function you pass to the action prop of a <form>. When the user submits, React calls the function with a FormData object. If the function is async, React tracks the submission until the returned promise settles.
useFormStatus reads that tracking. While the action runs, the status reports that the form is pending. When the promise settles, the status goes back to idle.
What the hook returns
Calling the hook returns an object with four fields.
| Field | Type | Meaning |
|---|---|---|
| `pending` | boolean | `true` while the parent form is submitting, otherwise `false` |
| `data` | `FormData` or `null` | The submitted data while pending, otherwise `null` |
| `method` | string | The form's HTTP method, `'get'` or `'post'`. The default is `'get'` |
| `action` | function, string, or `null` | The value of the parent form's `action` prop |
You will use pending most often. A later chapter covers the other three fields.
A first working example
The component that calls the hook must be rendered inside the form. Here, SubmitButton is a child of the <form>, so it can read the form's status.
'use client';
import { useFormStatus } from 'react-dom';
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? 'Saving...' : 'Save'}
</button>
);
}
export default function ProfileForm() {
async function saveProfile(formData) {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log('Saved', formData.get('name'));
}
return (
<form action={saveProfile}>
<input name="name" placeholder="Name" />
<SubmitButton />
</form>
);
}Follow what happens when the user clicks Save:
- React calls
saveProfilewith the form'sFormData. SubmitButtonre-renders withpendingset totrue. The button is disabled and reads "Saving...".- After one second the promise resolves.
pendingreturns tofalseand the button is enabled again.
Notice that ProfileForm has no state and passes no props to SubmitButton. The button gets its information from the nearest parent form.
The one rule to remember now
useFormStatus reads the status of a parent <form>. It does not read the status of a form rendered by the same component that calls the hook. If you call it in ProfileForm itself, pending will always be false. That is why the example puts the hook in SubmitButton.
What it does not do
useFormStatus is a read-only view of a submission. Keep these limits in mind:
- It does not start a submission. The form's
actiondoes that. - It does not return the result of the action. For a returned value, use
useActionState, covered later in this tutorial. - It reports only the nearest parent form. It cannot read a form elsewhere on the page.
- It does not report validation state. It only reports whether a submission is in progress.
Exercise
A teammate writes import { useFormStatus } from 'react' and gets an error that the export does not exist. What is the fix, and in which component should the hook be called for pending to work?
Show answer
Import it from react-dom: import { useFormStatus } from 'react-dom'. Call it in a component rendered inside the <form>, such as a SubmitButton child, and not in the component that renders the <form> itself.
Exercise
Given const { pending, data, method } = useFormStatus(); inside a child of a form that is not submitting, what values do you expect for pending and data?
Show answer
pending is false and data is null. The submitted FormData is only available while a submission is pending.