Sub topic 4
Why the Hook Must Live in a Child of the Form
This chapter assumes you know React function components and hooks, JSX and props, basic HTML forms with FormData, and async functions. You should also know that passing a function to a form's action prop starts a submission, which earlier chapters covered.
The rule
useFormStatus reads the status of a parent <form>. It does not take a form as an argument, and it does not look at forms rendered by the same component. React finds the form by walking up the component tree from the component that called the hook. If it finds no form above that component, the hook returns pending: false every time.
So the component that renders <form> cannot call useFormStatus for that form. A component rendered inside the form can.
A broken example
This is the most common mistake. The hook and the form live in the same component.
import { useFormStatus } from 'react-dom';
// Incorrect: the hook runs in the same 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 BrokenForm runs, the <form> has not been rendered yet. The hook looks above BrokenForm for a form and finds none. The button is never disabled, and the form can be submitted again while the first submission is still running.
The fix: move the hook into a child
Extract the part of the UI that needs the status into its own component. 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>. When the user submits, React runs saveProfile. While that async function is unfinished, pending is true for every component below the form that reads the hook. When the function resolves, pending returns to false.
The child does not need to be a direct child. Any depth works, as long as the form is an ancestor in the tree. A button wrapped in a layout div, or in several custom components, still sees the form.
What counts as "the parent form"
Three details cause confusion in practice.
- The form must use a function as its
action. The hook reports pending state for submissions handled through theactionprop. A form with anonSubmithandler and no functionactiondoes not report pending state through this hook. - The nearest form wins. The hook reports the closest ancestor
<form>. HTML does not allow nested forms, so this matters mostly when a component library renders its own form wrapper. - Siblings and parents do not count. Only descendants of the form can read its status. A component placed next to the form, or above it, gets the default values.
The table summarizes where the hook works.
| Where the hook is called | Reads the form's status? |
|---|---|
| In the component that renders `<form>` | No, `pending` stays `false` |
| In a component rendered inside the `<form>` | Yes |
| In a component nested several levels inside the `<form>` | Yes |
| In a sibling component outside the `<form>` | No |
| In a parent component above the form | No |
Passing the form to other components
A common reason to read the status from a child is to show feedback somewhere in the form, not only on the button. Any child can do this. This component shows a notice while the form submits.
import { useFormStatus } from 'react-dom';
function SubmissionNotice() {
const { pending, data, method } = useFormStatus();
if (!pending) return null;
return (
<p role="status">
Sending {data?.get('name')} with {method}...
</p>
);
}Rendering <SubmissionNotice /> anywhere inside the form gives it access to the same status. Later chapters cover data and method in detail.
Debugging checklist
If pending never becomes true, check these in order.
- Find the component that calls
useFormStatus. Is it the same component that returns<form>? If so, move the hook into a child. - Confirm the calling component is rendered inside the
<form>in the JSX tree, not beside it. - Confirm the form's
actionprop is a function, not a URL string or a missing value. - Confirm the action is actually asynchronous. A synchronous action finishes so fast that you may never see
pendingastrue.
Exercise
A teammate writes a component that calls useFormStatus and then returns a <form action={save}> with a submit button that uses pending. The button never disables during submission. What is wrong, and how do you fix it?
Show answer
The hook runs in the same component that renders the form, so there is no parent form above it and pending is always false. Move the button into its own component, such as SubmitButton, call useFormStatus there, and render <SubmitButton /> inside the <form>.
Exercise
You place a <SubmissionNotice /> component directly after the closing </form> tag, as a sibling. Will it show the pending state? Why or why not?
Show answer
No. Only components rendered inside the form, at any depth, can read its status. A sibling outside the form gets the default values, so pending stays false.