Sub topic 3
Reading the Pending Flag
This chapter assumes you can write React function components with hooks, read JSX and props, and use async functions and promises. It also assumes you know that FormData holds a form's named field values. The earlier chapters introduced form actions and the useFormStatus hook. Here the focus is on one value the hook returns, pending: when it is true, when it is false, and how to use it well.
The examples use React 19 or later. useFormStatus is imported from react-dom, not from react.
What the flag tells you
useFormStatus returns an object, and its pending field is a boolean. It answers one question: is the parent form currently running a form action?
pendingisfalsebefore the user submits.pendingbecomestruewhen the user submits a form whoseactionis a function.pendinggoes back tofalsewhen that action has finished and React has rendered the result.
You do not set this flag yourself. React sets it for you, and it applies to every component rendered inside that form that calls the hook. You can only read it.
const { pending } = useFormStatus();Treat pending as derived state. Do not copy it into your own useState. A copy can drift out of sync, and you would have to write the code that keeps it in step.
The lifecycle of a submission
The sequence below shows what happens from the click to the final render. The numbers in the diagram match the timeline described after it.
Two points in this timeline matter in practice.
- The button renders with
pending = trueas part of the submission itself. You do not need a click handler to start it. pendingreturns tofalseonly after the action has settled and React has applied the resulting update. If the action also changes other state, the new state and the cleared pending state arrive in the same render.
Reading pending in a component
Here is a small form that reads the flag and also logs every change, so you can watch it flip. Run it, open the browser console, and submit.
'use client';
import { useEffect } from 'react';
import { useFormStatus } from 'react-dom';
function PendingLogger() {
const { pending } = useFormStatus();
useEffect(() => {
console.log('pending changed to', pending);
}, [pending]);
return null;
}
function SaveButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? 'Saving...' : 'Save note'}
</button>
);
}
export default function NoteForm() {
async function saveNote(formData) {
await new Promise((resolve) => setTimeout(resolve, 1500));
console.log('Saved note:', formData.get('note'));
}
return (
<form action={saveNote}>
<textarea name="note" rows={3} />
<SaveButton />
<PendingLogger />
</form>
);
}You should see pending changed to false on mount, then true when you submit, then false about 1.5 seconds later.
Both PendingLogger and SaveButton are children of the <form>, and both see the same value. This is why the hook is useful. You can put a status indicator anywhere inside the form without passing props down.
What counts as pending
Not every form submission sets the flag. The flag follows React's form action mechanism, not the browser's submit event.
| How the form is submitted | Does `pending` become true? |
|---|---|
| `action` is an `async` function | Yes, until the returned promise settles and React renders the result |
| `action` is a synchronous function | Yes, but only for one render, which is usually too short to see |
| `action` is a Server Action | Yes, until the server responds and React renders the result |
| `action` is a function from `useActionState` | Yes, and the hook also reports its own `isPending` |
| `onSubmit` handler only, with no `action` function | No. React does not track this submission as a form action |
The last row catches many people. If you write <form onSubmit={handleSubmit}> and call event.preventDefault(), React is not running an action. A child using useFormStatus will read pending as false for the whole request. To use the flag, move your logic into a function passed to action.
// This form never reports pending, because it uses onSubmit.
<form
onSubmit={async (event) => {
event.preventDefault();
await save(new FormData(event.currentTarget));
}}
>
<SaveButton />
</form>
// This form does, because it uses the action prop.
<form
action={async (formData) => {
await save(formData);
}}
>
<SaveButton />
</form>Here save stands for whatever function sends your data. Only the second form sets pending.
Using the flag to drive the interface
A pending value is useful only if the interface responds to it. The common responses are listed below, from most to least essential.
- Disable the submit button. This prevents repeat clicks while the request is in flight.
- Change the label. Text such as "Saving..." tells the user the click registered.
- Mark the busy state for assistive technology. Use
aria-busyon the button, or a status region, so screen reader users also learn that work is happening. - Show a spinner or skeleton. This is optional, and it should not be the only signal.
A button that does the first three:
'use client';
import { useFormStatus } from 'react-dom';
export default function SubmitButton({ children, pendingLabel = 'Working...' }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending} aria-busy={pending}>
{pending ? pendingLabel : children}
</button>
);
}You would use it as <SubmitButton pendingLabel="Saving...">Save</SubmitButton>. A later chapter builds this into a full reusable component, so this version is only a sketch.
Announcing the state to screen readers
A label that changes from "Save" to "Saving..." is not always announced. Screen readers often do not read out a change to a button the user is already focused on, and a disabled button can lose focus in some browsers. A live region fixes this because it is read out when its content changes.
'use client';
import { useFormStatus } from 'react-dom';
export function SaveStatus() {
const { pending } = useFormStatus();
return (
<p role="status">
{pending ? 'Saving your changes...' : ''}
</p>
);
}Render <SaveStatus /> inside the form. The element with role="status" should be in the page from the first render, with its text changing, rather than being added and removed. Some screen readers only announce changes to a region that already exists.
Preventing double submissions
Disabling the button when pending is true stops most repeat submissions. Two details are worth knowing.
- A disabled submit button also blocks submission by pressing Enter in a text field. According to the HTML specification, if a form's default button is disabled, implicit submission does nothing. This only holds when the form has a submit button. A form with no submit button can still be submitted with Enter.
- Disabling the button protects only the client. A user can open the page in two tabs, retry from a flaky network, or send the request outside your page. If a duplicate submission would cause harm, such as charging a card twice, the server must handle it too. A common approach is an idempotency key sent as a hidden field.
Very fast actions and flicker
A response that arrives in 80 milliseconds makes pending flip to true and back almost at once. The button label flashes "Saving..." and then reverts. This looks worse than showing nothing.
You cannot fix this by delaying the flag, because the flag is not yours to change. Delay the visual instead, using CSS so that short pending states never become visible.
.spinner {
animation: show-after-delay 0.2s linear 0.3s both;
opacity: 0;
}
@keyframes show-after-delay {
to {
opacity: 1;
}
}This spinner stays invisible for 300 milliseconds and then fades in. If the action finishes sooner, the spinner is removed before the user sees it. Keep the button disabled for the whole pending period, though. Only decorative elements should be delayed.
When the action fails
If the action function throws or its promise rejects, pending still returns to false. The flag tracks whether the action is running, not whether it succeeded.
React treats an uncaught error from a form action as an error during rendering. It is passed to the nearest error boundary, and the component tree below that boundary is replaced by its fallback UI. That is rarely what you want for a validation failure such as "email already taken."
For expected failures, catch them in the action and return a value instead of throwing. Returning a value requires useActionState, which a later chapter covers. The key point for now is that pending does not tell you whether the submission worked.
'use client';
import { useFormStatus } from 'react-dom';
function StatusLine() {
const { pending } = useFormStatus();
// Wrong assumption: pending === false does NOT mean "saved successfully".
return <p>{pending ? 'Saving...' : 'Done'}</p>;
}This component shows "Done" before the first submission, and also after a failed one. Do not use pending alone to show a success message.
The initial render and the first value
useFormStatus returns an object even when nothing has been submitted. In that case pending is false. The other fields, such as data, are null. That means you do not need to guard against undefined before reading pending, and you can use it directly in a condition.
If the hook is called outside any form, it also returns pending as false. It does not throw an error. This behavior is the source of one of the most common mistakes: the flag silently stays false when the component is not where you think it is. The next chapter explains the rule about child components and shows how to diagnose it.
Quick reference
- Read it as
const { pending } = useFormStatus();with the import fromreact-dom. - It is
trueonly while a function passed toactionis running, plus the render that applies its result. - It is
falsebefore submission, after completion, after an error, and when no form is found. - Use it to disable the button, change the label, and set
aria-busy. Use arole="status"region for announcements. - Do not copy it into state, and do not treat
falseas proof of success.
Exercise
A teammate writes <form onSubmit={handleSubmit}> with event.preventDefault() and an inner SubmitButton that reads pending from useFormStatus. The button never disables during the request. What is the cause, and what is the smallest change that fixes it?
Show answer
The form is not running a React form action. onSubmit with preventDefault bypasses the mechanism that sets pending, so the hook always reads false. Move the request logic into a function that takes formData and pass it as action={...} on the form. Remove onSubmit and preventDefault. The SubmitButton then reads pending as true while the returned promise is unsettled.
Exercise
An action sometimes finishes in about 60 ms. Users report that the "Saving..." label flashes and looks broken. You want to keep the button disabled while pending, but avoid the flash. What do you do, and why can you not simply delay the pending value?
Show answer
Keep disabled={pending} for the whole pending period, and delay only the visual indicator, for example a spinner that uses a CSS animation with a 300 ms delay so it fades in only if the action is still running. You cannot delay pending itself because React controls it. useFormStatus is read-only, and copying it into your own state to delay it adds extra code and can fall out of sync.
Exercise
A component shows "Done" whenever pending is false. Describe two situations where this message is wrong.
Show answer
It is wrong before the user has submitted anything, because pending starts as false. It is also wrong after the action throws or fails, because pending returns to false whether or not the submission succeeded. A real success message needs a value returned from the action, for example through useActionState.