Skip to content
HooksChapter 25 / 37Advanced4 min read

Actions, useActionState & useOptimistic

Learn how useActionState, useOptimistic and useFormStatus handle form submits, pending state, errors and optimistic UI with React 19 Actions.

On this page

1. What are Actions?

In React 19 you can write <form action={fn}>. When the form is submitted, React calls fn with the FormData, and resets uncontrolled fields when the work is done.

Three helper hooks come with it:

  • useActionState: the result of the Action (like an error message) and a pending flag.
  • useOptimistic: updates the UI immediately, before the server replies.
  • useFormStatus (from react-dom): the pending status in any component inside a form.

2. Why does it matter?

Old form codeWith Actions
preventDefault in onSubmitThe form action takes a function directly
Three useState hooks for isLoading, error and datauseActionState gives you all of it in one place
Pass a pending prop to the submit buttonuseFormStatus lets the button read pending itself
The like count is old until the server repliesuseOptimistic shows the new count immediately

3. Where is it used?

Use caseTool
A login or signup form with errorsuseActionState
A like button, posting a commentuseOptimistic
A reusable submit button that shows "Saving..."useFormStatus
Calling a Next.js Server Function from a form<form action={serverFn}>

4. How does it work?

The form Action flow
The user submits the form
React runs the action with the FormData inside a transition
isPending is true and useFormStatus pending is true
The useOptimistic value shows on screen right away
The Action finishes, the new state arrives and the optimistic value is replaced by the real one

If the Action fails, the optimistic value automatically goes back to the real state.

5. Code example

useActionState + useFormStatus

tsx
"use client";

import { useActionState } from "react";
import { useFormStatus } from "react-dom";
import { updateName } from "./actions";

type State = { error: string | null };

function SubmitButton() {
  const { pending } = useFormStatus();
  return <button disabled={pending}>{pending ? "Saving..." : "Save"}</button>;
}

export function NameForm() {
  const [state, formAction, isPending] = useActionState(
    async (prev: State, formData: FormData): Promise<State> => {
      const name = String(formData.get("name") ?? "");
      if (name.length < 3) {
        return { error: "Name must be at least 3 characters" };
      }
      await updateName(name);
      return { error: null };
    },
    { error: null }
  );

  return (
    <form action={formAction}>
      <input name="name" disabled={isPending} />
      <SubmitButton />
      {state.error && <p>{state.error}</p>}
    </form>
  );
}

useOptimistic: an instant like

tsx
"use client";

import { useOptimistic, startTransition } from "react";
import { likePost } from "./actions";

export function LikeButton({ postId, likes }: { postId: string; likes: number }) {
  const [optimisticLikes, addLike] = useOptimistic(likes, (current, amount: number) => current + amount);

  function handleLike() {
    startTransition(async () => {
      addLike(1);
      await likePost(postId);
    });
  }

  return <button onClick={handleLike}>Like {optimisticLikes}</button>;
}

How does the code work?

PartMeaning
useActionState(fn, initial)Returns [state, formAction, isPending]
fn(prev, formData)The first argument is the previous state, the second is the FormData
<form action={formAction}>React runs the Action on submit
useFormStatus()Only works in a component inside the form
useOptimistic(likes, reducer)A temporary value while the Action runs
startTransition(async ...)The optimistic update must happen inside an Action

6. Common mistakes

Mistake: Calling useFormStatus in the same component that renders the form

Why? useFormStatus only gives the status of its parent form, not of a form in the same component.

Better: Make the submit button a separate component and render it inside the form.

Mistake: Calling the useOptimistic setter outside a transition

Why? An optimistic update needs to know when to end, and React warns you if you call it outside.

Better: Call addOptimistic inside a form action or inside startTransition.

Mistake: Forgetting the order of the Action function's arguments

Why? The action for useActionState takes the previous state first and FormData second, so formData.get returns undefined.

Better: Remember the signature: action(prevState, formData).

Interview tip

Remember the difference: useActionState handles the result, useOptimistic handles the UI while you wait, and useFormStatus tells a child component that the form is pending. The old name was useFormState, which is now useActionState.

7. What to learn before and after

Final quick revision

Hook or APIFromJob
<form action={fn}>React DOMRuns an Action on submit, with FormData
useActionStatereact[state, formAction, isPending]
useOptimisticreactTemporary UI before the reply
useFormStatusreact-dompending and data in a child component