Sub topic 5
Building a Reusable SubmitButton
This chapter assumes you know React function components, hooks, and props. It also assumes you know how HTML forms and FormData work, and that you can read async functions. You should already know that passing a function to a form's action prop starts a submission, and that useFormStatus reports pending for the parent form. This chapter turns that into a component you can reuse in every form.
Why a reusable button
Most forms in an app need the same behavior. The submit button should disable itself while a submission runs. It should also show progress text. If you copy that logic into each form, the labels drift apart and some forms forget to disable the button.
A SubmitButton component keeps this behavior in one place. It works because useFormStatus reads the status of the nearest parent <form>. The button never needs a pending prop from its parent.
A first version
Start with the smallest working component.
'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>, so pending is true for the full second that saveProfile runs. The button disables itself, which also blocks a second click from starting a duplicate submission.
The hook is imported from react-dom, not react. This is a common slip.
Making the labels configurable
The first version hardcodes "Save" and "Saving...". A reusable button should accept its labels as props. Keep the defaults so most callers pass nothing.
'use client';
import { useFormStatus } from 'react-dom';
export default function SubmitButton({
children = 'Submit',
pendingText = 'Submitting...',
...rest
}) {
const { pending } = useFormStatus();
return (
<button
type="submit"
disabled={pending}
aria-disabled={pending}
{...rest}
>
{pending ? pendingText : children}
</button>
);
}A caller can now write <SubmitButton pendingText="Publishing...">Publish</SubmitButton>. The ...rest spread passes through props such as className or form. Place it carefully. Because it comes after disabled, a caller who passes disabled would override the pending state. If you want the button to disable when either condition is true, combine them explicitly:
'use client';
import { useFormStatus } from 'react-dom';
export default function SubmitButton({
children = 'Submit',
pendingText = 'Submitting...',
disabled = false,
...rest
}) {
const { pending } = useFormStatus();
return (
<button
type="submit"
{...rest}
disabled={pending || disabled}
aria-disabled={pending || disabled}
>
{pending ? pendingText : children}
</button>
);
}Here disabled is pulled out of rest, then merged after the spread so neither value is lost.
Using it with a Server Action
The same component works when the form's action is a Server Action. The form can live in a Server Component. Only SubmitButton needs 'use client', because it calls a hook.
// 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 SubmitButton from './SubmitButton';
export default function NewPostForm() {
return (
<form action={createPost}>
<input name="title" required />
<SubmitButton pendingText="Publishing...">Publish</SubmitButton>
</form>
);
}NewPostForm has no state and no hooks, so it stays on the server. The small button is the only client code that ships to the browser.
Checklist for the component
- Keep
type="submit"so the button triggers the form's action. - Call
useFormStatusinsideSubmitButton, never in the component that renders the<form>. - Give the pending label a default, so callers can skip it.
- Merge a caller's
disabledwithpendinginstead of letting one replace the other. - Pass other props through with
...rest.
Exercise
You write <SubmitButton pendingText="Sending...">Send</SubmitButton> with the merged version above, and pass disabled={!isValid} where isValid is false. During a submission, what is the button's disabled value and label? Before any submission starts, what is the disabled value?
Show answer
During a submission, pending is true, so pending || disabled is true. The button is disabled and shows "Sending...". Before a submission starts, pending is false but disabled is true because isValid is false, so the button is still disabled and shows "Send".
Exercise
A teammate's SubmitButton always shows "Save" and never disables. The form is <form action={save}><SubmitButton /></form>, and the useFormStatus call sits in the component that renders this <form>. What is the fix?
Show answer
Move the useFormStatus call into SubmitButton, which is a child of the form. The hook only reports the status of a parent form, so calling it in the component that renders the form leaves pending as false.