How do you show a pending or loading state while a form is being submitted using a server action?
Short answer
You use the useFormStatus hook inside a child component of the form, which tells you whether the form is currently submitting, letting you disable the submit button or show a loading spinner while the server action is running, improving the experience for users on slower…
Advertisement
AModel answer
You use the useFormStatus hook inside a child component of the form, which tells you whether the form is currently submitting, letting you disable the submit button or show a loading spinner while the server action is running, improving the experience for users on slower connections.
'use client';
import { useFormStatus } from 'react-dom';
function SubmitButton() {
const { pending } = useFormStatus();
return <button disabled={pending}>{pending ? 'Sending...' : 'Send'}</button>;
}Real-world example
A checkout form disables its submit button and shows a spinner the moment a customer clicks pay, preventing them from accidentally submitting their order twice while it processes.
Likely follow-ups
- Why does useFormStatus need to be used in a child component rather than the form component itself?
- What other information does useFormStatus provide besides pending?
Related topics