Sub topic 8
Pending UI with Server Actions
This chapter assumes you know React function components, the action prop on a form, and the basics of useFormStatus from the earlier chapters. You should also know what Server Components and Client Components are. The examples use a framework that supports React Server Components, such as Next.js with the App Router.
What changes with a Server Action
In earlier chapters the form action was an ordinary function that ran in the browser. A Server Action is an async function that runs on the server. You mark it with the 'use server' directive, and React turns a call to it into a network request.
The pending state matters more here. A browser-only action may finish in a few milliseconds. A Server Action includes a round trip, and it may also include a database write. The user needs feedback during that time.
useFormStatus works the same way as before. It reports pending as true from the moment the form submits until the action finishes and React updates the page. The hook does not care where the action runs.
The three files
A Server Component can render the form and pass the Server Action to action. The submit button needs a hook, so it must be a Client Component. This splits the feature into three small files.
The Server Action:
// actions.js
'use server';
export async function createPost(formData) {
const title = formData.get('title');
// Save the post to a database here.
}The form, which is a Server Component because it has no directive and uses no hooks:
// NewPostForm.jsx
import { createPost } from './actions';
import SubmitButton from './SubmitButton';
export default function NewPostForm() {
return (
<form action={createPost}>
<input name="title" required />
<SubmitButton />
</form>
);
}The button, which is a Client Component:
// SubmitButton.jsx
'use client';
import { useFormStatus } from 'react-dom';
export default function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending} aria-disabled={pending}>
{pending ? 'Publishing...' : 'Publish'}
</button>
);
}SubmitButton is rendered inside the <form> that NewPostForm returns, so it satisfies the child component rule. The form component does not need to be a Client Component. Only the small piece that reads the status needs to be one, which keeps most of the page on the server.
What happens during a submission
The button re-renders twice. The first render shows Publishing... and disables the button. The second restores Publish once the server responds and React has applied the result.
Reading data and method with a Server Action
The data field holds the FormData that was submitted. This lets a child show what is being sent without lifting state into a parent.
'use client';
import { useFormStatus } from 'react-dom';
export function SubmissionNotice() {
const { pending, data } = useFormStatus();
if (!pending) return null;
return <p role="status">Publishing "{data?.get('title')}"...</p>;
}Place <SubmissionNotice /> inside the form, next to the button. The role="status" attribute makes screen readers announce the text politely when it appears.
For a Server Action, the action field of the status is a reference to the Server Action rather than a plain function. Most code never needs it. pending and data cover the common cases.
Behavior before hydration
Frameworks that support Server Actions let a form submit even before the page's JavaScript has loaded. The browser sends a normal form request, and the server runs the action. In that moment no hook is running, so no pending UI appears. Once the page hydrates, React takes over submissions and useFormStatus works as described.
This means pending UI is an enhancement, not a requirement for the form to work. Do not rely on pending to block duplicate submissions on the server. If duplicates would cause harm, make the Server Action safe to repeat, for example by checking for an existing record.
Common mistakes
- Reading the hook in the form component. If
NewPostFormitself calleduseFormStatus,pendingwould stayfalse. The hook reads the status of a parent form, so the call must be in a child. - Forgetting
'use client'. A hook cannot run in a Server Component. Without the directive inSubmitButton.jsx, the build reports an error. - Importing from the wrong package.
useFormStatuscomes fromreact-dom, notreact. - Putting
'use server'in the wrong file. The directive belongs at the top of the actions file, or inside the action function body. Client Components cannot define Server Actions inline, so import them from a'use server'file. - Expecting pending after a failed navigation. If the action calls
redirect, the page changes and the form unmounts, so the button never visibly returns to its idle state. This is normal.
Exercise
Your NewPostForm is a Server Component that renders a form with a Server Action. You want a pending label on the submit button. Which file needs 'use client', and why?
Show answer
The file that calls useFormStatus, which is SubmitButton.jsx. Hooks cannot run in Server Components. NewPostForm can stay a Server Component because it only renders the form and the button. SubmitButton is rendered inside the form, so the hook reads that form's status.
Exercise
A teammate adds const { pending } = useFormStatus() directly inside NewPostForm and disables the button with it. The button never disables. What is wrong, and how do you fix it?
Show answer
useFormStatus reads the status of a parent form, and NewPostForm is the component that renders the form, so there is no parent form above it. pending stays false. Move the hook into a child component such as SubmitButton, render that component inside the form, and mark it with 'use client'.