Writing

ReactJavaScriptFrontend · 6 min read

A Practical Guide to React 19 Actions

How many times have you written manual isLoading or isPending state checks just to show a spinner during a form submission? React 19 completely changes this experience by introducing native Actions, turning asynchronous operations into first-class citizens.

The Async State Mess

Before React 19, handling mutations in forms required writing repetitive boilerplate code. You had to:

  1. Initialize a loading state hook: const [isPending, setIsPending] = useState(false)
  2. Set it to true when submission begins: setIsPending(true)
  3. Wrap the async fetch call in a try/catch block
  4. Reset setIsPending(false) in the finally block
  5. Manually render loading spinners on buttons and disable inputs

This manual coordination was error-prone, led to visual glitches, and increased component complexity.

Enter React 19 Actions

In React 19, if you pass an asynchronous function to a DOM element, React automatically manages the pending state, error boundaries, and optimistic updates. Any function that performs an async action is known as an Action.

React automatically tracks the execution of your async function and provides hooks to interact with its state.

The useActionState Hook

To handle form state, React 19 introduces useActionState (which was previously called useFormState in experimental builds). It takes your action function and an initial state, returning the updated state, the wrapped action function, and a boolean indicating if the action is currently executing.

import { useActionState } from 'react';

// An async server action
async function updateProfile(prevState: any, formData: FormData) {
  try {
    const name = formData.get("username");
    await api.post("/profile/update", { name });
    return { success: true, message: "Profile updated!" };
  } catch (err) {
    return { success: false, message: "Failed to update." };
  }
}

function ProfileForm() {
  const [state, formAction, isPending] = useActionState(updateProfile, null);

  return (
    <form action={formAction} class="space-y-4">
      <input name="username" type="text" required disabled={isPending} />
      
      <button type="submit" disabled={isPending}>
        {isPending ? "Saving..." : "Update Profile"}
      </button>

      {state && (
        <p class={state.success ? "text-green-500" : "text-red-500"}>
          {state.message}
        </p>
      )}
    </form>
  );
}

Accessing Pending States with useFormStatus

What if a nested submit button needs to know if the parent form is pending, but you don't want to prop-drill the isPending boolean? React 19 provides the useFormStatus hook.

This hook acts like a context reader, automatically reading the status of the nearest parent <form>:

import { useFormStatus } from 'react-dom';

function SubmitButton() {
  const { pending, data, method, action } = useFormStatus();

  return (
    <button type="submit" disabled={pending} class="btn">
      {pending ? "Submitting..." : "Save Changes"}
    </button>
  );
}

[!NOTE] useFormStatus must be called from a component that is rendered inside a <form>. It will not read form status if called in the same component that declares the <form>.

Optimistic UI Updates

React 19 also ships with useOptimistic to make user actions feel instantaneous. It lets you display the final result of an operation before the server call actually finishes, then rolls back automatically if the action fails.

import { useOptimistic } from 'react';

function ChatList({ messages }) {
  const [optimisticMessages, addOptimisticMessage] = useOptimistic(
    messages,
    (state, newMessage: string) => [...state, { text: newMessage, sending: true }]
  );

  async function formAction(formData: FormData) {
    const text = formData.get("message") as string;
    addOptimisticMessage(text);
    await sendMessageToDatabase(text); // Async action
  }

  return (
    <div>
      {optimisticMessages.map((m, i) => (
        <p key={i} class={m.sending ? "opacity-50" : ""}>{m.text}</p>
      ))}
      <form action={formAction}>
        <input name="message" type="text" />
        <button type="submit">Send</button>
      </form>
    </div>
  );
}

Practical Takeaways

When applying React 19 Actions in your codebase:

  • Use action directly on <form> elements instead of registering onSubmit handlers.
  • Adopt useActionState to encapsulate status management, return messages, and input states.
  • Rely on useFormStatus inside child components to dynamically style buttons and loaders.
  • Pair mutations with useOptimistic for high-frequency actions like liking posts or sending chats.

Conclusion

React 19 Actions remove loading boilerplate, streamline state updates, and make application experiences feel responsive. By native tracking of async flows, React lets you write simple, declarative code that handles edge cases automatically.

  • Automated loaders: Say goodbye to manual isPending hooks.
  • Context-aware controls: Leverage useFormStatus for clean child component integrations.
  • Optimistic performance: Use useOptimistic to hide server latency.

Ready to try React 19 Actions? Convert your next form submission to use useActionState and see how much boilerplate you can delete!

All writing