# Form State (useActionState)

> Connecting Server Action results directly to UI state.

Subject: Next.js | Lesson 33 of 65 | 7 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-action-state
Previous: [Server Actions (Non-Forms)](https://codewithanimation.in/nextjs/server-actions-non-forms.md) | Next: [Pending States (useFormStatus)](https://codewithanimation.in/nextjs/use-form-status.md)

## The Server-to-Client Bridge

`useActionState` is a React Hook that allows you to update a component's state based on the result of a Server Action.

When a user submits a form, the action runs on the server and returns a value (like an error message or a success object). This hook automatically pipes that return value straight into your UI.

## Handling Validation Errors

Without this hook, handling server-side validation is tedious. You would have to manually intercept the form submission, track a pending state, and manually update a state variable with the server's response.

`useActionState` handles all of this declaratively while preserving Progressive Enhancement.

## Wiring the Hook

You initialize it with `const [state, formAction, isPending] = useActionState(myAction, initialState)`.

Crucially, the Server Action signature must change. It now receives the `previousState` as its first argument, and the `FormData` as its second argument: `async function myAction(prevState, formData)`.

## Common mistake: The Argument Order Trap

A very common error when adopting `useActionState` is forgetting to update the Server Action's parameter list.

If you forget to add `prevState` as the first argument, you will mistakenly try to extract data from the state object instead of the `FormData` object, causing immediate runtime crashes.

## Tip: Built-in Pending State

In modern React (19+), `useActionState` also returns an `isPending` flag as the third item in the array.

This completely eliminates the need for separate `isSubmitting` state variables, allowing you to easily disable buttons or show spinners while the action resolves.

## Try it: The Validation Loop

Try registering a username. If you use a taken username like 'admin', the Server Action will reject it and return an error state. Watch how `useActionState` instantly pipes that server error directly into the input field's UI.

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

## Check yourself

### Question 1

When using `useActionState`, what must the signature of your Server Action look like?

- A. async function myAction(formData)
- B. async function myAction(prevState, formData)
- C. async function myAction(formData, prevState)
- D. async function myAction(event)

Correct answer: B

When wrapped in `useActionState`, the action always receives the current state as its first argument, and the submitted FormData as its second.

- Why not A: This is the signature for a standard form action, but useActionState injects the previous state first.
- Why not C: The previous state is always the first argument.
- Why not D: Server Actions receive FormData, not DOM events.

### Question 2

Does `useActionState` preserve Progressive Enhancement?

- A. Yes, because it returns a `formAction` that can be passed directly to the `<form action={}>` attribute.
- B. No, it relies heavily on React's virtual DOM.
- C. Yes, but only if you don't use the `isPending` flag.
- D. No, hooks strictly require JavaScript to execute form submissions.

Correct answer: A

Yes. `useActionState` returns a specially decorated action that you pass to `<form action={formAction}>`. If JavaScript is disabled, it acts as a standard POST endpoint.

- Why not B: It intercepts submissions if JS is enabled, but falls back to native POST otherwise.
- Why not C: The pending flag just won't trigger without JS, but the form still submits.
- Why not D: The returned action string acts as an endpoint if JS is disabled.

## Remember this

- `useActionState` updates UI state based on the return value of a Server Action.
- It is primarily used for returning server-side validation errors to the client.
- The hook returns `[state, formAction, isPending]`.
- The Server Action MUST be updated to accept `(prevState, formData)`.
- It preserves Progressive Enhancement, falling back to a native POST if JS is disabled.
