Sub topic 6
Showing pending feedback with isPending
This chapter assumes you know React function components and useState, the FormData API, and async functions. You should also have seen the useActionState signature from the earlier chapters.
What isPending reports
useActionState returns three values: the current state, a wrapped action to pass to a form, and a boolean called isPending. The boolean is true from the moment a submission starts until the action's promise settles and React commits the new state.
This replaces a common hand-written pattern. Without the hook you would keep a loading flag in useState, set it to true before the request, and set it back to false in a finally block. With useActionState, React tracks that for you, and the flag cannot get stuck if you forget the finally.
A worked example
The component below disables the button and changes its label while the action runs. The action waits briefly to stand in for a network call.
import { useActionState } from "react";
async function save(prevState, formData) {
const title = String(formData.get("title") ?? "").trim();
await new Promise((resolve) => setTimeout(resolve, 1500));
return { savedTitle: title };
}
export function SaveForm() {
const [state, formAction, isPending] = useActionState(save, {
savedTitle: "",
});
return (
<form action={formAction}>
<input name="title" type="text" disabled={isPending} />
<button type="submit" disabled={isPending}>
{isPending ? "Saving..." : "Save"}
</button>
{isPending && <p aria-live="polite">Saving your changes</p>}
{state.savedTitle && <p>Saved: {state.savedTitle}</p>}
</form>
);
}Three things to notice:
- The button is disabled while
isPendingistrue, which prevents accidental double clicks. - The label changes, so sighted users see that something is happening.
- The status text uses
aria-live="polite", so screen readers announce it.
Choosing what to disable
Disabling controls has a side effect you should know about. A disabled input is not included in FormData. In the example above, the input is disabled after React has already built the FormData from the submit event, so the value is still sent. But if you disabled an input while it was still needed for a later submission, its value would be missing. Disable the submit button first, and disable inputs only when you want to block editing during the request.
You can also use isPending for lighter feedback:
- Dim the form with a CSS class or
data-pendingattribute. - Show a spinner next to the button.
- Keep the button enabled and ignore extra clicks, if you want repeated submissions to be queued instead.
Common mistakes
- Reading
isPendingin the action. The action runs outside rendering, so it cannot see the flag. Only the component reads it. - Expecting a flicker-free state. A fast action may flip
isPendingon and off within one frame. Avoid layout shifts by reserving space for the status text. - Blocking only with the label. Changing the text does not stop a second click. Pair it with
disabled. - Using it in a child component without a prop.
isPendingbelongs to the component that called the hook. A deeply nested button needs it passed down, or you can useuseFormStatus, which a later chapter covers.
Exercise
A user double-clicks the Save button in the example. What stops a second submission, and what would happen if you removed disabled={isPending} from the button?
Show answer
While isPending is true the button is disabled, so the second click does nothing. Without disabled, the second click would submit again. React would queue that submission and run it after the first finishes, so the save would happen twice.
Exercise
Why is it safer to read isPending from useActionState than to keep your own loading flag in useState?
Show answer
React sets and clears isPending for you around the action, including when the action throws. A hand-written flag depends on you resetting it in every code path, and it can stay true if you forget a finally block.