Sub topic 9
Reading status in child components with useFormStatus
This chapter assumes you know React function components and hooks, and that you have used useActionState with a form action. You should also be comfortable with FormData and async functions. The chapter shows how a child component can read the pending state of its parent form without receiving isPending as a prop.
The problem with passing isPending down
useActionState returns isPending as its third value. That works well when the button lives in the same component as the hook. In a real app, the submit button is often a shared component used in many forms. Passing isPending to it through props means every form must wire that prop by hand.
useFormStatus removes that wiring. A component reads the status of the nearest parent <form>. It does not care who created the form's action.
Using useFormStatus
useFormStatus is exported from react-dom, not from react. It returns an object with these fields:
| Field | Meaning |
|---|---|
| `pending` | `true` while the parent form's action is running |
| `data` | The `FormData` being submitted, or `null` when idle |
| `method` | The HTTP method of the form, `"get"` or `"post"` |
| `action` | The function or URL passed to the form's `action` prop |
Here is a reusable button:
import { useFormStatus } from "react-dom";
function SubmitButton({ children }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Saving..." : children}
</button>
);
}Now place it inside any form:
import { useActionState } from "react";
import { useFormStatus } from "react-dom";
function SubmitButton({ children }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Saving..." : children}
</button>
);
}
async function saveTitle(prevState, formData) {
const title = String(formData.get("title") ?? "").trim();
if (!title) {
return { error: "Title is required.", title };
}
await new Promise((resolve) => setTimeout(resolve, 800));
return { error: null, title: "" };
}
export function TitleForm() {
const [state, formAction] = useActionState(saveTitle, {
error: null,
title: "",
});
return (
<form action={formAction}>
<input name="title" type="text" defaultValue={state.title} />
<SubmitButton>Save</SubmitButton>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}TitleForm never reads isPending. The button finds out on its own.
How the status flows
The hook reads from the form that contains the component in the render tree. React tracks that form's action as a transition, and every useFormStatus call inside the form sees the same status.
Common errors
The most frequent mistake is calling useFormStatus in the same component that renders the <form>. It then returns pending: false every time, because the hook only looks at a form that is a parent of the calling component.
import { useFormStatus } from "react-dom";
// Wrong: this component renders the form, so the hook does not see it.
function BrokenForm({ action }) {
const { pending } = useFormStatus(); // always false
return (
<form action={action}>
<button type="submit" disabled={pending}>Send</button>
</form>
);
}The fix is to move the hook into a child component, like SubmitButton above.
Other points to remember:
- The hook reports only the form that wraps the component. It does not report on sibling forms.
- If the component is not inside any form,
pendingisfalse. pendingreflects a formaction. A dispatcher called from an event handler, covered in a later chapter, does not set it for a form.
Using the data field
The data field holds the FormData of the active submission. You can use it to show what is being sent, for example a "Saving 'Hello'..." label:
import { useFormStatus } from "react-dom";
function StatusLine() {
const { pending, data } = useFormStatus();
if (!pending) return null;
return <p>Saving "{String(data?.get("title") ?? "")}"...</p>;
}Exercise
Why does a component that renders <form action={formAction}> and calls useFormStatus itself always see pending as false, and how do you fix it?
Show answer
useFormStatus reads the status of a form that is a parent of the calling component. A component that renders the form is not inside it, so it finds no form. Move the hook into a child component rendered inside the form, such as a SubmitButton.
Exercise
Write a FieldsetStatus component that disables its fieldset while the parent form is submitting. Where must it be placed?
Show answer
Call const { pending } = useFormStatus(); and return <fieldset disabled={pending}>{children}</fieldset>. Render it inside the <form>, as a child of a component that sits within the form, and import useFormStatus from react-dom.