Sub topic 6
Showing pending feedback with isPending
This chapter covers the third value useActionState returns, isPending, and how to use it to give users feedback while a submission is running. It assumes you can write a basic action function with useActionState (previous state and FormData in, next state out). It also assumes you know that React 19 form actions run inside transitions.
What isPending is
useActionState returns an array of three values:
const [state, formAction, isPending] = useActionState(action, initialState);isPending is a boolean. It is true from the moment React starts running your action until the action's promise settles and the new state is committed. Otherwise it is false.
You don't write any code to track this. With useState, you would do something like this:
- Call
setLoading(true)before the request. - Call
setLoading(false)in afinallyblock. - Remember to handle every exit path.
With useActionState, React derives the flag from the action's lifecycle, so it can't get stuck on true because a finally block was missed.
The lifecycle of a submission
Two details matter here:
statedoes not change while the action runs. It still holds the previous result, so any error message from the last attempt stays on screen until the action finishes. Clear or replace it deliberately if that is not what you want.isPendingand the newstateupdate together in the same render. You will not see a frame where the pending flag isfalseand the old state is still showing.
A complete example
This component simulates a slow request, so the pending state is visible. You can paste it into any React 19 project.
import { useActionState } from "react";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function saveProfile(prevState, formData) {
const name = String(formData.get("name") ?? "").trim();
await sleep(1500); // stand-in for a network request
if (!name) {
return { status: "error", message: "Name is required.", name };
}
return { status: "saved", message: `Saved ${name}.`, name };
}
export function ProfileForm() {
const [state, formAction, isPending] = useActionState(saveProfile, {
status: "idle",
message: "",
name: "",
});
return (
<form action={formAction} aria-busy={isPending}>
<label>
Name
<input name="name" type="text" defaultValue={state.name} />
</label>
<button type="submit" disabled={isPending}>
{isPending ? "Saving..." : "Save"}
</button>
<p role="status">
{isPending ? "Saving your profile..." : state.message}
</p>
</form>
);
}The component uses isPending in three ways:
- Disable the button.
disabled={isPending}stops the user from clicking again while a request is in flight. - Change the label. "Saving..." tells the user the click registered.
- Mark the form busy.
aria-busytells assistive technology the form is updating. Therole="status"paragraph is a live region, so screen readers announce the text change.
What to do with the flag
Pick the lightest feedback that fits the action.
| Technique | Good for | Watch out for |
|---|---|---|
| Disable the submit button | Preventing duplicate submissions | Disabled buttons lose focus, which can confuse keyboard users |
| Change the button label | Short actions (under a few seconds) | Layout shift if the new label is wider |
| Show a spinner or progress text | Slow actions | Needs a live region to be announced |
| Dim the form or set `aria-busy` | Whole-form updates | `aria-busy` alone shows nothing visually |
| Disable inputs | Making clear the values are locked | See the next section |
Disabling inputs
You can write disabled={isPending} on inputs too. React captures the FormData when the form is submitted, so disabling inputs after that does not remove their values from the data your action receives. The action still sees what the user typed.
There is one catch with a related pattern. If you disable an input in the same render that the submission starts, the browser has already built the form data, so this is safe. But a field that is disabled when the user submits is left out of FormData entirely. Keep that in mind if you disable fields for other reasons.
Setting readOnly instead of disabled keeps the field focusable and keeps it in FormData. It is often the gentler choice.
isPending does not cover everything
isPending reports only the action. It says nothing about:
- Work you do before the action starts, such as client-side checks in an
onClickhandler. - Data your action triggers elsewhere, such as a refetch that starts after the action returns.
- The first load of the page.
If your action kicks off follow-up work, wait for that work inside the action, so isPending stays true until it is done:
async function saveAndRefresh(prevState, formData) {
await fetch("/api/profile", { method: "POST", body: formData });
const fresh = await fetch("/api/profile").then((r) => r.json());
return { profile: fresh };
}The pending flag now covers both requests, and the new state arrives in a single commit.
Pending with repeated submissions
If a user submits again while an action is running, React does not run the actions at once. Each call is queued and runs after the previous one finishes, with each one receiving the state returned by the one before it. isPending stays true for the whole run of queued actions and becomes false only after the last one finishes.
This is why disabling the button is still worth doing. The queue keeps your state consistent, but it will happily run the same submission three times if the user clicks three times. A disabled button is the simplest guard. It is not a security measure, and the server should still handle duplicate requests safely.
isPending outside a form
isPending is not tied to the <form> element. If you call the dispatcher from an event handler, wrap the call in startTransition. The flag works the same way:
import { startTransition, useActionState } from "react";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function toggleLike(prevLiked) {
await sleep(800);
return !prevLiked;
}
export function LikeButton() {
const [liked, dispatch, isPending] = useActionState(toggleLike, false);
return (
<button
disabled={isPending}
onClick={() => startTransition(() => dispatch())}
>
{isPending ? "Updating..." : liked ? "Unlike" : "Like"}
</button>
);
}Without startTransition, React warns that an action was dispatched outside a transition, and isPending will not update as you expect.
Pending feedback in child components
You may have a reusable button deep inside a form. Passing isPending down as a prop works, but it adds plumbing. useFormStatus reads the same information from the nearest parent form without a prop. A later chapter covers it. For now, remember that isPending belongs to the component that called useActionState.
Common mistakes
- Using the flag to hide an error. Because
statekeeps its old value during a submission, a stale error message sits next to a "Saving..." label. Decide whether to hide the message while pending, as the example above does. - Reading
isPendingand expecting it to betruein the action. The action runs outside rendering. It cannot readisPending. If the action needs to know it is running, it already does. - Throwing from the action to signal failure. If the action throws,
isPendingbecomesfalseand the error goes to the nearest error boundary. For expected problems such as validation, return an error in state instead. - Assuming a short action shows the pending UI. If the action finishes within a frame or two, users may see a flash of "Saving..." or nothing at all. If the flicker bothers you, keep the layout stable so the label change does not move other elements.
- Disabling the button and losing the Enter key. In browsers, pressing Enter in a text field triggers the form's default submit button. If that button is disabled, implicit submission is blocked. That is usually what you want during a pending submission, but test it, because it also means the user can't submit with Enter until the action finishes.
Summary of the key points
isPendingis the third element returned byuseActionState. It istruewhile the action runs.- The flag comes from the transition React wraps around the action, so you never set it yourself.
statekeeps its previous value until the action finishes.- Queued submissions keep
isPendingtrueuntil the last one completes. - Outside a form, call the dispatcher inside
startTransition.
Exercise
A user double-clicks a "Save" button that is wired to useActionState but is not disabled while pending. The action takes two seconds. What happens, and what is the value of isPending during the second action?
Show answer
React queues the second submission and runs it after the first finishes, passing it the state returned by the first. isPending stays true for the whole four seconds and becomes false only after the second action completes. Disabling the button with disabled={isPending} prevents the second click from being queued at all.
Exercise
You call dispatch(1) from an onClick handler without wrapping it in startTransition. Which of the three returned values stops behaving as expected, and how do you fix the call?
Show answer
isPending does not update as expected, and React warns that the action was dispatched outside a transition. Wrap the call: onClick={() => startTransition(() => dispatch(1))}.
Exercise
Your action calls await fetch("/api/save") and then, after returning, a separate effect refetches the list. Users see the button re-enable before the list updates. How do you make the pending state cover both?
Show answer
Move the refetch into the action and await it before returning, then return the fresh data as the new state. isPending then stays true until both requests finish, and the new state commits in one render.