Published on

Understanding useActionState in React

Authors
  • Mehdi Akiki avatar
    Name
    Mehdi Akiki
    Twitter

React’s useActionState is an experimental hook available in the Canary and experimental channels. It’s designed to manage form state updates when the form action is triggered. Think of it like a more advanced version of useState that works directly with form actions.

When you submit a form, useActionState updates the state based on the return value of your action function, making it easier to manage form submissions. This is especially useful when working with Server Components, where you want the form to be interactive even before the JavaScript bundle loads.

Example of useActionState

Here is a simple example:

import { useActionState } from "react";

async function updateUsername(prevUsername, formData) {
  const newUsername = formData.get("username");
  return newUsername;
}

function UsernameForm() {
  const [username, formAction] = useActionState(updateUsername, "Guest");

  return (
    <form>
      <label>
        Username:
        <input name="username" defaultValue={username} />
      </label>
      <button formAction={formAction}>Update Username</button>
      <p>Current Username: {username}</p>
    </form>
  );
}

In this example, every time you submit the form, the state is updated with the new username entered in the input field. useActionState provides the current username (username) and a new action (formAction) that you attach to the form's button. When the form is submitted, the action updates the state with the new username.

useState vs. useActionState

  • useState: Handles local component state. You directly update the state with setter functions, and it's very flexible.

  • useActionState: Specifically built for handling state based on form submissions. It works well with form actions and can handle server-side actions before client-side JavaScript has fully loaded.

When to Use useActionState

Use useActionState when working with forms in Server Components, especially if you want the form to be interactive right after the page loads, even before hydration is complete. It’s ideal for managing state tied to form submissions and simplifies server-side form handling.

Conclusion

In short, useActionState enhances form handling by letting you update component state based on form submissions. While similar to useState, it’s specialized for forms and plays nicely with Server Components, making your forms responsive and state-aware right from the start.

I build and scale reliable production systems. Open to full-time and freelance work with U.S.-based teams that value ownership and execution.

Got something in mind?

Book a Discovery Call