useActionState
useActionState manages state produced by Actions.
const [state, dispatchAction, isPending] = useActionState(
reducerAction,
initialState,
);
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β
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.
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β
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
keywhen 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.
Progressive enhancement and permalinkβ
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β
const [cart, cartAction, isPending] = useActionState(saveCart, initialCart);
const [optimisticCart, updateOptimisticCart] = useOptimistic(cart, cartReducer);
They are complementary, not competing Hooks.
Common mistakesβ
- Treating
useActionStateas a replacement foruseState. - Putting ordinary modal/tab/hover state in side-effectful Action logic.
- Forgetting sequential queue semantics.
- Returning incompatible state shapes.
- Calling
dispatchActionoutside an Action/Transition. - Throwing expected validation errors when the UI should render them as state.
Production decision guideβ
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β
- https://react.dev/reference/react/useActionState
- https://react.dev/reference/react/useTransition
- https://react.dev/reference/react-dom/components/form
Nextβ
Continue with Form Actions and useFormStatus.