Forms and pending states

Intermediate · 14 min

Show a pending state with useActionState or useFormStatus, bind a form to a Server Action, and use the Form component for GET search forms.

Before thisServer Actions basics

ByteGrad · 14 min

Forms and pending states

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by ByteGradReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A Server Action takes a long time compared with a click, so the form needs to say it is working; the pending boolean from useActionState or useFormStatus drives that.
  • useActionState returns state, a wrapped action, and a pending flag, and it needs a Client Component because the form must read that state.
  • useFormStatus reads the status of the nearest parent form, so it only works inside a child component rendered within the form, such as a submit button.
  • next/form's Form component with a string action behaves like a GET form: it puts the fields in the URL, prefetches the target, and does a client-side transition.
  • Disabling a submit button during pending prevents duplicate submissions from a double click.

A silent form feels broken

Click a button and nothing changes for 400 milliseconds. Most people click again. With a Server Action, that second click sends a second request, and you might create two posts.

The browser does not tell you a request is running once a form has been intercepted by React, so the form has to say so itself. React provides two hooks that expose that status.

useActionState: state plus pending

useActionState wraps your action and gives back three things: the latest state the action returned, a wrapped action for the form, and a pending boolean.

'use client'
import { useActionState } from 'react'
import { createUser } from '@/app/actions'

const initialState = { message: '' }

export function Signup() {
  const [state, formAction, pending] = useActionState(createUser, initialState)

  return (
    <form action={formAction}>
      <label htmlFor="email">Email</label>
      <input type="text" id="email" name="email" required />
      <p aria-live="polite">{state?.message}</p>
      <button disabled={pending}>Sign up</button>
    </form>
  )
}

The form’s component has to be a Client Component, because it reads state. The action’s signature changes too: it now receives the previous state as its first argument, then the FormData. The aria-live="polite" region tells screen readers about the message when it appears. The next lesson uses the state for validation errors.

useFormStatus: pending from a child

If you only need pending, and no returned state, useFormStatus from react-dom reads the status of the form it sits inside.

'use client'
import { useFormStatus } from 'react-dom'

export function SubmitButton() {
  const { pending } = useFormStatus()
  return (
    <button disabled={pending} type="submit">
      Sign up
    </button>
  )
}
<form action={createUser}>
  {/* fields */}
  <SubmitButton />
</form>

The hook looks at the parent form, so a component that renders the <form> itself cannot call it for that form. Hence the separate button component. In React 19 the returned object also has data, method, and action.

Which one? If you already need useActionState for returned state, take pending from it. If the form is otherwise a Server Component and you only want a disabled button, useFormStatus in a tiny client button is lighter.

Forms that do not mutate: next/form

Some forms only change the URL, like a search box. For those, Next.js has a Form component. With a string action, it behaves like a native GET form.

import Form from 'next/form'

export default function Search() {
  return (
    <Form action="/search">
      <input name="query" />
      <button type="submit">Search</button>
    </Form>
  )
}

On submit the fields go into the query string (/search?query=abc). Next.js prefetches the target path when the form is visible, preloading shared layouts and loading states, and does a client-side navigation instead of a full reload, so client state is kept. Pass a function as action and it behaves like a React form running a Server Action. The results page reads searchParams, which the dynamic APIs lesson covers.

Pending UI for work that is not a form

An action can also be triggered from an event handler. The docs show useActionState with startTransition so a plain button gets a pending flag. The same flag also feeds optimistic updates. The React-side details are in forms and controlled inputs and useTransition and useDeferredValue.

What the video covers

ByteGrad’s February 2025 video compares the ways to build a form in Next.js. By its chapters: why forms are used for data and search, a plain HTML form, a Server Action, useActionState, the Form component, and a form-backend service. The last segment is a sponsored promotion for that backend, which this lesson does not rely on. useFormStatus is not a chapter, so the lesson and the Codevolution runner-up cover it.

Where this fits

A form that shows pending is half of a good mutation experience. The other half is saying what went wrong. Next: validation and error handling in actions.

Check yourself

  1. 01Why does useFormStatus not work when called in the same component that renders the form?

    It reads the status of a parent form, so it must be called from a component rendered inside the form, which is why a separate SubmitButton component is the usual shape.

  2. 02What are the three values returned by useActionState?

    The current state, a wrapped action to pass to the form's action prop, and a pending boolean that is true while the action runs.

  3. 03When would you use the Form component from next/form instead of a Server Action?

    For forms that only change the URL, such as search, because a string action makes a GET form that appends the fields as search params, prefetches the target, and does a client-side transition.

  4. 04What bug does disabling the button while pending prevent?

    A user clicking twice and submitting the same data twice before the first request returns.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark