Q30: How does the useActionState Hook manage form submission state in React 19?

useActionState (formerly useFormState) simplifies form handling by accepting an async action function and returning the updated state, form action trigger, and a isPending status flag.

useActionState Form Submission
import { useActionState } from 'react';

async function updateNameAction(previousState, formData) {
  const name = formData.get('name');
  return await api.updateName(name);
}

function Profile() {
  const [state, formAction, isPending] = useActionState(updateNameAction, null);
  return (
    <form action={formAction}>
      <input name="name" />
      <button disabled={isPending}>Save</button>
      {state?.error && <p>{state.error}</p>}
    </form>
  );
}

useActionState manages isPending states and server action responses without manual useState calls.

Key Takeaways
  • Handles pending status, server responses, and form errors natively.

  • Integrates seamlessly with Server Actions and HTML <form action> elements.

Knowledge Check

Which flag returned from useActionState indicates that a form Server Action is actively pending?