# Pending States (useFormStatus)

> Context-aware pending states for nested form components.

Subject: Next.js | Lesson 34 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-form-status
Previous: [Form State (useActionState)](https://codewithanimation.in/nextjs/use-action-state.md) | Next: [Optimistic Updates (useOptimistic)](https://codewithanimation.in/nextjs/use-optimistic.md)

## Implicit Form Context

`useFormStatus` is a React Hook that gives you access to the status of a form submission.

Under the hood, native `<form>` elements in modern React act like Context Providers. Any child component inside the form can use this hook to instantly know if the form is currently submitting.

## Reusable Submit Buttons

Historically, to disable a submit button while loading, you had to manage an `isSubmitting` state at the top of your component and pass it down as a prop: `<SubmitButton disabled={isSubmitting} />`.

With `useFormStatus`, the button manages itself. You can build a generic `<SubmitButton>` that automatically disables itself and shows a spinner whenever the parent form is pending.

## The Pending Flag

Inside your child component, call the hook: `const { pending } = useFormStatus();`.

Use that boolean to conditionally style the button or disable it to prevent double-submissions: `<button disabled={pending}>`.

## Common mistake: The Hierarchy Trap

The most common mistake is calling `useFormStatus` in the SAME component that renders the `<form>` tag.

Because the hook consumes context, it MUST be called in a child component rendered *inside* the form. If you call it at the top level alongside the form, `pending` will simply remain false forever.

## Tip: Data and Action Retrieval

In addition to `pending`, the hook also returns `{ data, method, action }`.

You can use `data` (which is a FormData object) to optimistically display the user's input before the server even responds!

## Try it: The Component Boundary

Submit the form below. Notice how the nested `<SubmitButton>` automatically detects the pending state and shows a spinner. The visual highlights the strict component boundary required to make the hook work.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/use-form-status

## Check yourself

### Question 1

Why will `useFormStatus` fail to update if called in the same component that renders the `<form>`?

- A. Because it only tracks client-side form validations.
- B. Because it acts as a Context consumer and must be used inside a child component of the form.
- C. Because you forgot to pass the Server Action to the hook directly.
- D. Because it is incompatible with the Next.js App Router.

Correct answer: B

Like React Context, the consumer (`useFormStatus`) must be a descendant of the provider (the `<form>` element). It cannot be on the same level.

- Why not A: It tracks Server Action submissions.
- Why not C: The hook takes no arguments; it reads from the DOM.
- Why not D: It is specifically designed for use with Server Actions in the App Router.

### Question 2

Will `useFormStatus` track a Server Action triggered via an `onClick` handler on a button outside of a form?

- A. Yes, it tracks any Server Action in the component tree.
- B. No, it only tracks actions dispatched by a `<form>` element.
- C. Yes, if you pass the button ID to the hook.
- D. No, because onClick handlers cannot trigger Server Actions.

Correct answer: B

The hook reads the status from the parent HTML `<form>`. If there is no form, or if the action is triggered manually via `onClick`, `useFormStatus` will not work.

- Why not A: It specifically tracks `<form>` submissions.
- Why not C: The hook takes no arguments.
- Why not D: They can, but they act as RPCs and bypass form context.

## Remember this

- `useFormStatus` reads pending state implicitly from a parent `<form>`.
- It is primarily used for creating reusable `<SubmitButton>` components.
- You MUST call it from a child component inside the form.
- Calling it in the same component that renders the form will result in `pending` always being false.
- It only works with form submissions, not manual `onClick` RPC mutations.
