What You'll Learn
You'll learn the exact function signature of useActionState and what each parameter and return value represents when managing async action state.
The Hook Signature
useActionState follows a straightforward pattern:
const [state, formAction, isPending] = useActionState(action, initialState);
Let's break down each piece.
The Parameters
action — An async function that represents your server action or form handler. It receives two arguments: the previous state and the form data (if triggered by a form).
initialState — The starting value for your state before any action runs. This could be an empty object, null, or any initial data structure you need.
The Return Values
The hook returns an array with three elements:
state — The current state value. After the action completes, this updates to whatever your action function returned. During the first render, it's initialState.
formAction — A wrapped version of your action function that you pass to a form's action prop. This wrapper handles the state transitions automatically.
isPending — A boolean flag that's true while the action is in flight and false otherwise. Perfect for showing loading spinners or disabling buttons.
The Mental Model
Think of useActionState like a traffic controller at a busy intersection. Your action is the car trying to cross. The hook tracks where the car is (pending), what cargo it carried last time (state), and gives you a special lane to send the next car through (formAction).
The signature is intentionally simple because React handles the complex state transitions internally — you just declare what should happen.
Key Takeaway: useActionState(action, initialState) returns [state, formAction, isPending], giving you everything needed to track an async action's lifecycle: the result, a form-ready function, and a loading flag.