Sub topic 7
Using data, method, and action from the form status
This chapter assumes you can write React function components and hooks, know basic HTML forms and FormData, and have read the earlier chapters on useFormStatus: the pending flag and the rule that the hook must be called from a child of the form. It covers the other three fields the hook returns: data, method, and action.
The shape of the returned object
useFormStatus always returns an object with four fields:
const { pending, data, method, action } = useFormStatus();The hook reads the status of the nearest parent <form>. When that form is not submitting, or when there is no parent form, the hook returns a frozen "not pending" object where pending is false and the other three fields are null.
| Field | Type while pending | Value when not pending |
|---|---|---|
| `pending` | `true` | `false` |
| `data` | `FormData` holding the submitted values | `null` |
| `method` | `'get'` or `'post'`, from the form's `method` attribute | `null` |
| `action` | The function passed to the form's `action` prop | `null` |
The fields change together. pending is the one you check first, because it tells you whether the other three are worth reading.
Reading data
data is the FormData object that React built from the form at the moment the user submitted it. It is the same kind of object your action receives as its argument. Any component inside the form can read the submitted values while the action runs, without prop drilling and without extra state.
A typical use is a status message that names what is being sent:
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>
);
}
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? 'Sending...' : 'Send'}
</button>
);
}
export default function ContactForm() {
async function send(formData) {
await new Promise((resolve) => setTimeout(resolve, 1500));
console.log('Sent', formData.get('name'));
}
return (
<form action={send} method="post">
<input name="name" placeholder="Name" />
<SubmissionNotice />
<SubmitButton />
</form>
);
}Both SubmissionNotice and SubmitButton are children of the form, so both can call the hook. Each call gets the same status for the same form.
What data.get returns
FormData.get(name) returns a FormDataEntryValue. That is a string for text inputs and a File for file inputs. It returns null if no field has that name. Three consequences follow:
- Optional chaining (
data?.get('name')) protects you if you render before checkingpending. Prefer an early return onpendinginstead. - A missing field gives
null, which renders as nothing in JSX. A typo in the field name therefore fails silently, so check thenameattribute first when a value does not appear. - A file field gives a
File, which you cannot render directly. Read itsnameorsizeproperty.
import { useFormStatus } from 'react-dom';
function UploadNotice() {
const { pending, data } = useFormStatus();
if (!pending) return null;
const file = data.get('avatar');
const label = file instanceof File && file.name ? file.name : 'your file';
return <p role="status">Uploading {label}...</p>;
}
export default function AvatarForm() {
async function upload(formData) {
await new Promise((resolve) => setTimeout(resolve, 2000));
console.log('Uploaded', formData.get('avatar')?.name);
}
return (
<form action={upload}>
<input type="file" name="avatar" />
<UploadNotice />
<button type="submit">Upload</button>
</form>
);
}When the user submits without choosing a file, the browser still sends a File entry with an empty name. The file.name check above handles that case.
data is a snapshot
data holds the values from the moment of submission. If the user keeps typing while the action runs, data does not change. This is useful because the notice describes what was actually sent, not what the inputs say now.
data also disappears when the action settles. React resets uncontrolled form fields after a successful function action, and useFormStatus returns to null values at the same point. If you need the submitted value to stay on screen after the action finishes, store it yourself. The action's return value with useActionState, covered in a later chapter, is the usual place for it.
Reading method
method is a string, 'get' or 'post', taken from the form element's method attribute. A form with no method attribute reports 'get', which is the HTML default.
With a function passed to action, React calls that function in JavaScript. It does not send an HTTP request with the form's method, so for client-side actions method is informational. It becomes more meaningful in two situations:
- A shared component serves several forms, some of them configured with
method="post"and others withmethod="get", and the UI should differ. - You are building a wrapper component that wants to log or label what kind of submission is happening.
A small example that adapts its wording:
import { useFormStatus } from 'react-dom';
function MethodLabel() {
const { pending, method } = useFormStatus();
if (!pending) return null;
return (
<span role="status">
{method === 'post' ? 'Saving changes...' : 'Searching...'}
</span>
);
}
export function SearchForm() {
async function search(formData) {
await new Promise((resolve) => setTimeout(resolve, 800));
console.log('Searched for', formData.get('q'));
}
return (
<form action={search}>
<input name="q" />
<MethodLabel />
<button type="submit">Search</button>
</form>
);
}
export function SettingsForm() {
async function save(formData) {
await new Promise((resolve) => setTimeout(resolve, 800));
console.log('Saved', formData.get('theme'));
}
return (
<form action={save} method="post">
<input name="theme" />
<MethodLabel />
<button type="submit">Save</button>
</form>
);
}In SearchForm, no method is set, so the component sees 'get'. In SettingsForm it sees 'post'. The same MethodLabel component works in both.
Because the default is 'get', an unset method is not a sign that something is broken. Do not read method === 'get' as "no method was configured."
Reading action
action is a reference to the function you passed to the form's action prop. Its main use is identity: a component can find out which action is running and respond accordingly.
If the form has no action prop, or action is a URL string, action is null. Those submissions are handled by the browser as normal page navigations, so there is no pending state to show.
The example below defines two actions and one shared status component. The component looks up its label by action identity:
import { useFormStatus } from 'react-dom';
async function saveDraft(formData) {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log('Draft saved', formData.get('body'));
}
async function publish(formData) {
await new Promise((resolve) => setTimeout(resolve, 2000));
console.log('Published', formData.get('body'));
}
const labels = new Map([
[saveDraft, 'Saving draft...'],
[publish, 'Publishing...'],
]);
function ActionStatus() {
const { pending, action } = useFormStatus();
if (!pending) return null;
return <p role="status">{labels.get(action) ?? 'Working...'}</p>;
}
export default function Editor() {
return (
<>
<form action={saveDraft}>
<textarea name="body" />
<ActionStatus />
<button type="submit">Save draft</button>
</form>
<form action={publish}>
<input type="hidden" name="body" value="Hello" />
<ActionStatus />
<button type="submit">Publish</button>
</form>
</>
);
}ActionStatus has no props. Each form's own status tells it which function is running, so one component serves both forms.
Keep action references stable
The comparison works only if the function you compare against is the same object that was passed to the form. Functions defined inside a component body are recreated on every render:
export default function Page() {
// A new function object on every render of Page.
async function save(formData) { /* ... */ }
return <form action={save}>{/* ... */}</form>;
}If the component re-renders while the action is pending, the action in the status still points to the function from the render that started the submission. A comparison against the current save then fails. Define actions you want to identify at module level, as saveDraft and publish are above, or export them from a module.
Several forms on one page
Each call to useFormStatus reads its own nearest parent form. Two forms on the same page do not share status, so a button in one form never sees the other form's submission. This makes list UIs straightforward, where each row has a form and only the row being submitted shows progress:
import { useState } from 'react';
import { useFormStatus } from 'react-dom';
function DeleteButton() {
const { pending, data } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? `Deleting ${data.get('title')}...` : 'Delete'}
</button>
);
}
export default function TaskList() {
const [tasks, setTasks] = useState(['Write docs', 'Fix bug', 'Ship release']);
async function remove(formData) {
await new Promise((resolve) => setTimeout(resolve, 1000));
const title = formData.get('title');
setTasks((current) => current.filter((task) => task !== title));
}
return (
<ul>
{tasks.map((task) => (
<li key={task}>
{task}
<form action={remove}>
<input type="hidden" name="title" value={task} />
<DeleteButton />
</form>
</li>
))}
</ul>
);
}Here data.get('title') comes from a hidden input. Using data for the label means DeleteButton needs no props. Every row reuses the same component and the same remove action. Inside DeleteButton, data is never null when pending is true, so reading it in the pending branch is safe.
Using the fields with Server Actions
Nothing about the fields changes when the action is a Server Action. The SubmitButton still has to be a Client Component, because hooks need the client. The form can stay in a Server Component:
// actions.js
'use server';
export async function createPost(formData) {
const title = formData.get('title');
// Save the post to a database here.
}
// NewPostForm.jsx
import { createPost } from './actions';
import PostStatus from './PostStatus';
export default function NewPostForm() {
return (
<form action={createPost}>
<input name="title" required />
<PostStatus />
<button type="submit">Publish</button>
</form>
);
}
// PostStatus.jsx
'use client';
import { useFormStatus } from 'react-dom';
export default function PostStatus() {
const { pending, data } = useFormStatus();
if (!pending) return null;
return <p role="status">Publishing "{data.get('title')}"...</p>;
}The data object here is built in the browser from the form before the request goes to the server, so it is available immediately. The server never sends it back to the status component.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| `Cannot read properties of null (reading 'get')` | Reading `data.get(...)` while `pending` is `false` | Return early when `!pending`, or use `data?.get(...)` |
| `data`, `method`, and `action` are always `null` | The hook is called in the component that renders the form, or outside any form | Move the hook into a child component rendered inside the `<form>` |
| `action` never matches the function you compare with | The action is defined inside a component body and recreated on each render | Define the action at module level |
| `action` is `null` while the form works | The `action` prop is a URL string or is missing | Pass a function to `action` if you want a pending state |
| A submitted value shows as empty | The input has no `name` attribute, or the name does not match the `get` call | Check the `name` attribute |
| The status text vanishes the moment the action ends | `data` returns to `null` after the action settles | Keep results in state, or return them from the action with `useActionState` |
The first row has a related trap. Writing const name = data.get('name') at the top of a component and checking pending afterwards will throw on every non-pending render. Check pending first.
Exercise
A reusable PendingNote component calls useFormStatus and renders Sending {data.get('email')}... whenever it is rendered inside a form. It crashes on the first render, before anyone has submitted. Why, and how do you fix it? Then explain what method would report for a form that has no method attribute.
Show answer
Before a submission, pending is false and data is null, so data.get('email') throws. Fix it by returning early with if (!pending) return null; before reading data (or by using data?.get('email')). For a form with no method attribute, method reports 'get' while a submission is pending, because 'get' is the HTML default. When nothing is pending it is null.
Exercise
You define async function save(formData) {...} inside your Profile component and pass it to <form action={save}>. A child component compares action === save to choose a label, but the comparison is false whenever Profile re-renders during a submission. What is the cause, and what change makes the comparison reliable?
Show answer
The function is recreated on every render, so the action in the status refers to the function from the render that started the submission, which is a different object from the current save. Move save outside the component, to module level or a separate module, so there is one stable function object. Then action === save holds across renders.
Exercise
Write a RowStatus component that, inside a form containing a hidden input named id, renders a paragraph with role="status" saying Updating item <id>... while the form is pending and renders nothing otherwise.
Show answer
import { useFormStatus } from 'react-dom'; function RowStatus() { const { pending, data } = useFormStatus(); if (!pending) return null; return <p role="status">Updating item {data.get('id')}...</p>; }