Q49: How does the useFormStatus Hook track parent Server Action pending states?

useFormStatus tracks the pending status of a parent <form>. Crucially, it must be called inside a child component nested beneath the <form>, functioning like a Context consumer.

Submit Button Component with useFormStatus
'use client';
import { useFormStatus } from 'react-dom';

export function SubmitButton() {
  const { pending } = useFormStatus(); // Reads parent <form> state
  return (
    <button disabled={pending}>
      {pending ? 'Saving Changes...' : 'Save'}
    </button>
  );
}

SubmitButton reads parent form pending status without requiring prop drilling.

Key Takeaways
  • Reads pending state, form data, and HTTP submission methods.

  • Must be invoked inside a child component nested within the form tags.

Knowledge Check

Where must a component calling useFormStatus() be located relative to the <form> element?