Sub topic 3
Reading the pending flag
This chapter covers the pending value that useFormStatus returns. It assumes you can write React function components with hooks, know JSX and props, understand HTML forms and FormData, and are comfortable with async functions and promises. It also assumes you know that a function passed to a form's action prop runs when the form is submitted.
What pending tells you
useFormStatus is imported from react-dom, not from react. It returns an object, and pending is the field you will use most often:
const { pending } = useFormStatus();pending is a boolean with two meanings:
true: the parent form has been submitted and its action has not finished yet.false: the form is idle. This is the value before any submission, and again after the action settles.
React sets pending to true when the submission starts. It sets it back to false when the promise returned by your action resolves or rejects. You do not set it yourself, and you do not need a useState flag with a try/finally block.
The lifecycle of one submission
The diagram shows the values pending takes during a single submit.
If the action is async, pending stays true until the returned promise settles. If the action is synchronous, pending flips to true and back so quickly that the user will not see it. The pending UI matters when the work is asynchronous: a network request, a database write, or a timer.
A first example
This is the smallest useful case. It reads pending and uses it to disable the button and change its label.
'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 aFormDataobject built from the form's fields. pendingbecomestrue.SubmitButtonre-renders, so the button is disabled and reads "Saving...".- After one second the promise resolves.
pendingbecomesfalseand the button returns to "Save".
Disabling the button also prevents a second click from starting a duplicate submission while the first is still running.
Check pending before using the other fields
useFormStatus also returns data, method, and action. Later chapters cover them in detail. For now, remember that they only carry useful values while a submission is in progress. When the form is idle, data is null. So check pending first, and treat the other fields as valid only after that check passes:
import { useFormStatus } from 'react-dom';
function SubmissionNotice() {
const { pending, data, method } = useFormStatus();
if (!pending) return null;
return (
<p role="status">
Sending {data?.get('name')} with {method}...
</p>
);
}The early return null keeps the notice out of the page when nothing is happening. It also means the data?.get('name') call only runs when a submission is underway.
What pending does not cover
pending describes one thing: whether the nearest parent form's action is running. These cases do not set it to true:
- A handler on
onSubmitoronClick. Only a function passed to the form'sactionprop is tracked. If you callevent.preventDefault()in anonSubmithandler and do your ownfetch,pendingnever changes. - A different form. Each form has its own status. A button reads the status of the form that contains it, not any other form on the page.
- The result of the action.
pendingsays that work is running, not whether it succeeded or what it returned. For a returned message or an error, useuseActionState, covered in a later chapter.
Using pending in other ways
The button is the most common place to read pending, but any child of the form can use it. Some options:
- Show a spinner or progress text next to the button.
- Disable other controls, such as a Cancel button or text inputs, so the user cannot edit values mid-submission.
- Add
aria-disabledor arole="status"message so assistive technology users also learn that the form is working.
Note that disabled={pending} on an input prevents its value from being included in a later submission, but the current submission already has its FormData. Disabling inputs after the submit event has fired is safe.
Quick reference
| Situation | Value of `pending` |
|---|---|
| Form has not been submitted | `false` |
| Async action running | `true` |
| Action's promise resolved or rejected | `false` |
| Synchronous action | `true` only for an instant |
| `onSubmit` handler doing its own `fetch` | Always `false` |
| Hook called in the component that renders the form | Always `false` |
Exercise
A developer writes <form onSubmit={handleSubmit}> where handleSubmit calls event.preventDefault() and then awaits a fetch. A child SubmitButton uses useFormStatus and disables itself when pending is true. The button never disables. Why, and what change makes pending work?
Show answer
useFormStatus only tracks submissions that run through the form's action prop. An onSubmit handler that prevents the default and does its own fetch bypasses that mechanism, so pending stays false. Move the work into an async function, accept a FormData argument, and pass it as <form action={handleSubmit}>. Then pending is true until the function's promise settles.