Sub topic 3
Using Form Status Fields for Pending UI
Using `data`, `method`, and `action` from the form status
useFormStatus returns more than pending. While a submission is in progress:
datais the submittedFormDataobject.methodis the form's HTTP method, such asgetorpost.actionis the action function or URL receiving the submission.
These fields are null or default values when no submission is active. Check pending before reading submitted values.
'use client';
import { useFormStatus } from 'react-dom';
function SubmissionNotice() {
const { pending, data, method, action } = useFormStatus();
if (!pending) return null;
return (
<p role="status">
Sending {data?.get('name')} with {method}.{' '}
{action ? 'Action is configured.' : 'No action is configured.'}
</p>
);
}
export default function ProfileForm({ saveProfile }) {
return (
<form action={saveProfile} method="post">
<input name="name" required />
<button type="submit">Save</button>
<SubmissionNotice />
</form>
);
}The notice must render inside the form. useFormStatus reports the nearest parent form, so it can support form-specific messages without passing state through props.
Pending UI with Server Actions
A Server Action is a function that runs on the server and can be passed to a form's action prop. The child button can remain a small Client Component.
'use server';
export async function createPost(formData) {
const title = formData.get('title');
// Validate title and save it on the server.
console.log(title);
}When to reach for `useActionState` instead
Use useFormStatus when a descendant mainly needs submission status. Use useActionState when the form owner also needs the action's returned result, such as validation errors or a success message. useActionState returns state, a form action, and its pending flag.
Common pitfalls: why `pending` stays false
- The hook runs in the component that renders the
<form>. Move it into a child. - The form does not use
action={function}or the child is outside the form. - The component is missing
'use client'where the hook is used. - The submission finishes before you can observe the pending UI.
- A custom click handler bypasses the form submission.
Exercise
Where should a component calling useFormStatus be rendered?
Show answer
Inside the form, as a descendant of the form whose status it should read.