Skip to main content

useActionState

useActionState manages state produced by Actions.

const [state, dispatchAction, isPending] = useActionState(
reducerAction,
initialState,
);
useReducer vs useActionStateThey both calculate next state, but they solve different lifecycle problems.
Pure UI transitionsuseReducer
Pure reducer Β· no side effects Β· synchronous state transition model.
Action result stateuseActionState
Reducer Action may be async Β· may perform side effects Β· exposes pending state Β· queues Action calls.

Basic example​

import {useActionState, startTransition} from 'react';

async function updateName(previousState, nextName) {
const response = await fetch('/api/profile', {
method: 'POST',
body: JSON.stringify({name: nextName}),
});

if (!response.ok) {
return {...previousState, error: 'Could not update name'};
}

const user = await response.json();
return {name: user.name, error: null};
}

function ProfileEditor() {
const [state, dispatchAction, isPending] = useActionState(
updateName,
{name: 'Ada', error: null},
);

function handleSave() {
startTransition(() => {
dispatchAction('Grace');
});
}

return (
<section>
<p>{state.name}</p>
<button disabled={isPending} onClick={handleSave}>
{isPending ? 'Saving…' : 'Save Grace'}
</button>
{state.error && <p role="alert">{state.error}</p>}
</section>
);
}

Reducer Action mental model​

How one dispatch becomes Action result state
Previous Action state
reducerAction(previousState, payload)
May perform side effects and may be async.
Next Action state

Unlike a useReducer reducer, the reducer Action can perform side effects:

async function cartAction(previousCart, payload) {
const savedCart = await saveCartChange(payload);
return savedCart;
}

Dispatch must happen inside an Action​

Wrong:

dispatchAction(payload); // ❌ outside an Action

Use startTransition:

startTransition(() => {
dispatchAction(payload);
});

Or pass the dispatcher to an Action prop such as a form:

<form action={dispatchAction}>
...
</form>

Pending state​

const [state, dispatchAction, isPending] = useActionState(action, initialState);

isPending belongs to the Action lifecycle for that Hook. Avoid duplicating the same concept with another isSubmitting flag unless it represents something different.

Sequential Action ordering​

Multiple dispatches are queued and processed sequentially.

Queued useActionState callsEach reducer Action receives the result of the previous call.
1Dispatch A
2Run A with previous state
3Result A
4Dispatch B consumes Result A
5Result B becomes state

This ordering is useful when each operation logically depends on the previous result.

async function quantityAction(previousQuantity, type) {
if (type === 'add') return await addOne(previousQuantity);
if (type === 'remove') return await removeOne(previousQuantity);
return previousQuantity;
}

If operations should run independently or in parallel, consider a different model such as useState + useTransition, request-specific state, a server-state library, or a domain-specific mutation queue.

Action payloads​

Payloads can be simple or structured:

dispatchAction({
type: 'rename',
productId: 'p-17',
name: 'Mechanical Keyboard',
});

Choose a shape that communicates intent; do not copy reducer conventions mechanically.

Form integration​

The returned Action can be passed directly to a form:

async function submitContact(previousState, formData) {
const email = formData.get('email');

if (!email) {
return {message: 'Email is required'};
}

await saveContact(email);
return {message: 'Saved'};
}

function ContactForm() {
const [state, formAction, isPending] = useActionState(
submitContact,
{message: null},
);

return (
<form action={formAction}>
<input name="email" type="email" />
<button disabled={isPending}>Save</button>
{state.message && <p>{state.message}</p>}
</form>
);
}

The reducer Action receives previousState first and the FormData payload second.

Expected vs unexpected errors​

Error ownership
Expected user/domain outcome
Validation error Β· out of stock Β· rejected business rule β†’ return structured Action state.
Unexpected failure
Programming/system failure β†’ throw when an Error Boundary and monitoring strategy should handle it.

Example expected outcome:

async function checkoutAction(previousState, formData) {
const result = await checkout(formData);

if (result.type === 'out-of-stock') {
return {
...previousState,
error: 'One item is no longer available',
};
}

return {error: null, orderId: result.orderId};
}

Keep the returned state shape stable and intentional.

Reset behaviour​

useActionState does not expose a generic reset function. Possible designs include:

  • dispatching an explicit reset payload;
  • remounting with a different key when identity should reset;
  • relying on appropriate successful form reset behaviour for browser-owned fields;
  • moving canonical state to a different owner when that better matches the architecture.

The optional permalink argument supports a specific React Server Components + Server Function progressive-enhancement scenario:

useActionState(action, initialState, '/profile');

Do not add it to ordinary client-only applications without that server-rendered workflow.

Server Functions are not required​

useActionState can also work with client-side async Actions. It is a React Action-state primitive; Server Functions add server integration and progressive-enhancement capabilities.

useActionState + useOptimistic​

Confirmed state and optimistic projection
useActionState
Owns confirmed Action result + pending lifecycle + ordered Action state.
useOptimistic
Projects an immediate temporary UI assumption while the Action is pending.
Canonical result wins
const [cart, cartAction, isPending] = useActionState(saveCart, initialCart);
const [optimisticCart, updateOptimisticCart] = useOptimistic(cart, cartReducer);

They are complementary, not competing Hooks.

Common mistakes​

  • Treating useActionState as a replacement for useState.
  • Putting ordinary modal/tab/hover state in side-effectful Action logic.
  • Forgetting sequential queue semantics.
  • Returning incompatible state shapes.
  • Calling dispatchAction outside an Action/Transition.
  • Throwing expected validation errors when the UI should render them as state.

Production decision guide​

Does useActionState fit this workflow?
State is specifically the result of a user Action?Good candidate
Need pending state tied to that Action?Good candidate
Sequential previous-result ordering is meaningful?Strong fit
Actions should run independently in parallel?Use a different mutation model
Need immediate temporary feedback?Pair with useOptimistic

Interview questions​

Junior: What are the three values returned by useActionState?

Mid-level: How is the function passed to useActionState different from a useReducer reducer?

Senior: Why are queued useActionState Actions sequential, and when would that behaviour be the wrong fit?

References​

Next​

Continue with Form Actions and useFormStatus.