Sub topic 4
Why the Hook Must Live in a Child of the Form
This chapter assumes you know React function components, props, and hooks, and that you have seen HTML forms and FormData. You should also be comfortable with async functions and promises. The previous chapters introduced form actions and the pending flag. This chapter explains the placement rule for useFormStatus and what goes wrong when you break it.
By the end you will be able to:
- State the placement rule precisely.
- Explain why a component that renders a
<form>cannot read that form's status. - Predict what the hook returns for any component, from its position in the tree.
- Debug a
pendingflag that never turnstrue.
The rule in one sentence
useFormStatus reports the status of the nearest parent <form> of the component that calls it. The calling component must be rendered inside that form, as a descendant in the React tree.
A component that renders the form is not inside it. It is the form's owner, so it falls outside the rule. The hook returns the default status for it, with pending: false, and React raises no error or warning.
The mistake everyone makes first
The most natural code puts the hook in the same component as the form. It reads well and looks correct:
import { useFormStatus } from 'react-dom';
// Incorrect: the hook runs in the component that renders the form,
// so it does not read this form's status and pending stays false.
function BrokenForm({ action }) {
const { pending } = useFormStatus();
return (
<form action={action}>
<input name="name" />
<button type="submit" disabled={pending}>Save</button>
</form>
);
}When the user submits, the action runs and the form is busy. The button never disables, because pending was false on every render. Nothing is wrong with the action, the button, or the props. The hook is called from the wrong place.
How the hook finds its form
You can think of useFormStatus as reading from a context, much like useContext. When React DOM renders a <form> whose action is a function, it makes that form's submission status available to everything rendered beneath it. The hook looks upward from the component that calls it and returns the status of the first form it meets.
A component's hook calls run while that component renders. At that moment, the component's own <form> element has not been rendered yet, so nothing has provided a status to it. The component sits above the form, and the hook looks upward from there. It finds either an outer form or nothing, and nothing gives it the default value.
Because the lookup goes upward only, the hook cannot see:
- A form rendered by the same component that calls the hook.
- A sibling form.
- A form nested in a child of the calling component.
The fix: move the hook into a child
Extract the part that needs the status into its own component, and render that component inside the form:
'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>
);
}SubmitButton is rendered inside the <form>, so its hook finds that form. While saveProfile is running, pending is true and the button disables itself and shows "Saving...". When the action finishes, pending goes back to false.
ProfileForm does not call the hook at all. It owns the form and the action, and the child reads the status. This split is the standard pattern, and the next chapter turns it into a reusable component.
Position in the tree, not in the file
The rule is about where a component is rendered, not where its code is written or which component's JSX contains it. A component receives the form's status if it appears anywhere beneath the form in the rendered tree.
This matters when a wrapper component renders the form and receives its contents through children:
'use client';
import { useFormStatus } from 'react-dom';
function StatusLine() {
const { pending } = useFormStatus();
return <p role="status">{pending ? 'Working...' : 'Idle'}</p>;
}
function Card({ action, children }) {
return (
<section>
<form action={action}>{children}</form>
</section>
);
}
export default function Page() {
async function run() {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
return (
<Card action={run}>
<button type="submit">Run</button>
<StatusLine />
</Card>
);
}StatusLine is written in Page's JSX, not inside Card's. It still works, because React renders it as a descendant of the <form> element that Card creates. The status line shows "Working..." during the action.
Likewise, a deeply nested component works. A SubmitButton can sit inside several wrapper divs and layout components and still read the form's status. It does not need to be a direct child, despite what the phrase "child of the form" suggests. Any descendant works.
Several forms on one page
Each call to the hook reads its own nearest form, so two forms on the same page do not interfere. One reusable button can serve both:
'use client';
import { useFormStatus } from 'react-dom';
function SubmitButton({ label }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? 'Working...' : label}
</button>
);
}
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export default function TwoForms() {
async function slow() {
await wait(3000);
}
async function quick() {
await wait(500);
}
return (
<div>
<form action={slow}>
<SubmitButton label="Slow save" />
</form>
<form action={quick}>
<SubmitButton label="Quick save" />
</form>
</div>
);
}Submitting the first form disables only the first button, for about three seconds. The second form stays interactive. This isolation is the benefit of reading the status from the surrounding form instead of from shared state.
Look-alikes that do not work
Some placements look like they should work but fall outside the rule.
A button linked with the form attribute
HTML lets a button outside a form submit it through the form attribute, which names the form's id. React's status lookup follows the component tree, not the HTML association:
'use client';
import { useFormStatus } from 'react-dom';
function OutsideButton() {
const { pending } = useFormStatus();
// pending stays false: this component is not rendered inside the form.
return (
<button type="submit" form="profile" disabled={pending}>
Save
</button>
);
}
export default function Page() {
async function save(formData) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
return (
<div>
<form id="profile" action={save}>
<input name="name" />
</form>
<OutsideButton />
</div>
);
}Clicking the button still submits the form, but the button never disables. The fix is to move the button inside the <form>. If the layout prevents that, read the pending state in the owner of the form, which is the job of useActionState (see the later chapter on choosing between the two hooks).
A parent that wraps the form
If a component calls the hook and renders a child that contains the form, the caller is still above the form. Moving the hook up the tree never helps. It has to move down.
A form without a function action
The status tracks submissions handled by a function passed to action. A form with a string action, or a handler on onSubmit that calls event.preventDefault(), does not report a pending status through this hook. Switch to a function action to use it.
No form at all
A component rendered outside every form gets the default status. This is useful to know when you reuse a button in several places: outside a form it simply reports pending: false.
Summary table of placements
| Where the hook is called | What it reads | Typical `pending` |
|---|---|---|
| In a component rendered inside the form, at any depth | That form | Real value |
| In the component that renders the `<form>` | No form (or an outer one) | Always `false` |
| In a component beside the form | No form (or an outer one) | Always `false` |
| In a component linked by the `form` attribute but rendered outside | No form (or an outer one) | Always `false` |
| In a child of one of two sibling forms | The form it is rendered in | Real value |
| Inside a form that has a string action or no function action | No tracked status | `false` |
Debugging a pending flag that stays false
Work through these checks in order:
- Find the component that calls
useFormStatus. Does it also render the<form>? If so, extract the hook call into a child component. - Check that the calling component is rendered somewhere beneath the
<form>in the tree. Search the JSX for where it is used, not only where it is defined. - Confirm the form's
actionis a function, not a URL string, and that you did not replace submission with anonSubmithandler that prevents the default. - Check the import. The hook comes from
react-dom, notreact. In frameworks with Server Components, the file that calls the hook needs'use client'at the top. - Make sure the action takes long enough to see. A fast action can finish before the user notices any pending state. Add a temporary delay while testing.
When the owner needs the status too
Sometimes the component that renders the form also needs to know about submission, for example to show a result message. The placement rule means it cannot use useFormStatus for that. The form's owner can use useActionState, which returns an isPending flag in the same component that owns the form:
'use client';
import { useActionState } from 'react';
async function subscribe(previousState, formData) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { message: 'Subscribed ' + formData.get('email') };
}
export default function SubscribeForm() {
const [state, formAction, isPending] = useActionState(subscribe, { message: '' });
return (
<form action={formAction}>
<input name="email" type="email" />
<button type="submit" disabled={isPending}>Subscribe</button>
<p>{state.message}</p>
</form>
);
}Here the owner reads isPending directly and no child component is needed. useFormStatus remains the better choice when a reusable component deep in the tree should react to whichever form surrounds it, without receiving props.
Exercises
Exercise
A teammate writes function Form({ action }) { const { pending } = useFormStatus(); return <form action={action}><button disabled={pending}>Go</button></form>; }. The button never disables during submission. Explain the cause and give the smallest fix.
Show answer
The hook is called in the component that renders the <form>, so it looks upward from a position above the form and finds no form status. pending is always false. The smallest fix is to move the button into a new child component, such as SubmitButton, that calls useFormStatus and is rendered inside the <form>.
Exercise
A SubmitButton that calls useFormStatus is placed in a layout component's JSX and passed as children to a Card component, which renders <form>{children}</form>. Will the button see the form's pending state? Why?
Show answer
Yes. The hook depends on where React renders the component, not where its JSX was written. SubmitButton is rendered as a descendant of the <form> element that Card creates, so it reads that form's status.
Exercise
A page has <form id="profile" action={save}> and, outside it, a button with form="profile" that calls useFormStatus. Clicking the button submits the form, but pending stays false. Why, and what are two ways to fix it?
Show answer
The form attribute links the button to the form in HTML only. The button component is not rendered beneath the form in the React tree, so the hook cannot find the form's status. Fix it by moving the button component inside the <form>, or by using useActionState in the component that owns the form and reading isPending there.