How does the useFormStatus hook help show loading feedback while a server action is running?
Short answer
useFormStatus gives you access to information about the nearest parent form's current submission state, including a pending flag, letting you disable a submit button or show a loading spinner automatically while a server action is still processing, without manually tracking that…
Advertisement
AModel answer
useFormStatus gives you access to information about the nearest parent form's current submission state, including a pending flag, letting you disable a submit button or show a loading spinner automatically while a server action is still processing, without manually tracking that state yourself.
import { useFormStatus } from 'react-dom';
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? 'Saving' : 'Save'}
</button>
);
}Real-world example
A comment form automatically disables its submit button and shows a saving message while a server action processes the new comment, using useFormStatus instead of manually managing a separate loading state.
Likely follow-ups
- Why must useFormStatus be called inside a component nested within the form, rather than the form component itself?
- How does this compare to manually tracking a loading state with useState?
Related topics