Sub topic 7
Using data, method, and action from the form status
This chapter assumes you can write React function components, know the basics of FormData, and have already built a child component that reads pending from useFormStatus. Here you will read the other three fields the hook returns.
What the hook returns
useFormStatus returns an object with four fields. You have used pending. The other three describe the submission that is in progress.
| Field | Value while pending | Value when idle or no parent form |
|---|---|---|
| `pending` | `true` | `false` |
| `data` | The `FormData` the user submitted | `null` |
| `method` | `'get'` or `'post'`, from the form's `method` attribute | `'get'` |
| `action` | The function passed to the form's `action` prop | `null` |
The values come from the closest parent <form>. As with pending, the component that calls the hook must be rendered inside the form, not be the component that renders it.
The diagram below shows when these fields change.
Reading data
data is the FormData object for the submission in progress. It lets a child component show what the user just sent, without lifting state out of the form. Because data is null when nothing is pending, check pending first.
'use client';
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>
);
}
export default function ProfileForm() {
async function saveProfile(formData) {
await new Promise((resolve) => setTimeout(resolve, 1500));
console.log('Saved', formData.get('name'));
}
return (
<form action={saveProfile} method="post">
<input name="name" placeholder="Name" />
<button type="submit">Save</button>
<SubmissionNotice />
</form>
);
}When the user submits, SubmissionNotice renders "Sending Ada with post..." for 1.5 seconds. It then returns null again. The optional chaining on data?.get('name') is a safety net. The early return already guarantees data is set in practice.
data is a snapshot of what was submitted. It does not track later edits to the inputs, and it is gone as soon as the submission ends. Do not use it to store a result. For that, see useActionState in a later chapter.
Reading method
method reports the HTTP method of the parent form as the string 'get' or 'post'. It reflects the form's method attribute, and a form without the attribute reports 'get'.
This surprises people. The form above uses a function as its action, so React calls that function and sends no HTTP request. The method value is still reported, but it does not change how the function runs. It is mostly useful for display, logging, or a reusable component that words its message differently for 'post' forms.
import { useFormStatus } from 'react-dom';
function MethodBadge() {
const { pending, method } = useFormStatus();
if (!pending) return null;
return <small>{method.toUpperCase()} in progress</small>;
}Reading action
action holds a reference to the function you passed to the form's action prop. A child can use it to tell which action is running, or to confirm that a form is wired up the way you expect.
import { useFormStatus } from 'react-dom';
function ActionDebug() {
const { pending, action } = useFormStatus();
if (!pending) return null;
return (
<p role="status">
Running {action?.name || 'anonymous action'}
</p>
);
}Inside a form with action={saveProfile}, this prints "Running saveProfile". Two cases give null instead of a function. One is a form whose action is a URL string. The other is a form with no action at all. Those forms submit through the browser's normal navigation, and useFormStatus does not track them.
Choosing which field to use
Reach for each field only when the UI needs it.
- Use
pendingfor disabling controls and showing progress. Almost every form needs this. - Use
datato echo what is being sent, such as "Adding Milk to your list...". This works well in a shared status component that does not know the field names ahead of time. - Use
methodrarely, for labels or diagnostics. - Use
actionmainly for debugging or for a shared component that behaves differently depending on which function it belongs to.
Common mistakes
- Reading
datawithout checkingpending. Outside a submissiondataisnull, sodata.get('name')throws. Return early whenpendingis false, or usedata?.get(...). - Calling the hook in the form's own component. All four fields then stay at their idle values. The hook must run in a child rendered inside the
<form>. - Expecting
datato persist after the action ends. It resets withpending. Keep results inuseActionStateor in component state. - Assuming
methodcontrols the request. With a function action, no HTTP request is built from it.
Exercise
A child component inside a form calls const { pending, data } = useFormStatus(); and renders <p>Saving {data.get('title')}</p> unconditionally. The page crashes on first render. Why, and how do you fix it?
Show answer
When no submission is in progress, data is null, so data.get('title') throws a TypeError on the first render. Fix it by returning early when pending is false (if (!pending) return null;) or by using optional chaining, data?.get('title').
Exercise
A form is rendered with <form action="/subscribe">, and a child component reads action from useFormStatus during a submission. What value does it get, and why?
Show answer
It gets null. The action field reports a function passed to the form's action prop. A URL string means the browser handles the submission itself, and useFormStatus does not track it.