useActionState is a React Hook that allows you to update state based on the result of a form action.
In earlier React Canary versions, this API was part of React DOM and called
useFormState.Parameters
(state: Awaited<S>, payload: P) => S
required
The function to be called when the form is submitted or button is pressed. When the function is called, it will receive the previous state of the form (initially the
initialState you pass, subsequently its previous return value) as its first argument, followed by the form data as its second argument.The action can be:- A synchronous function that returns the next state
- An async function (Server Action) that returns a Promise of the next state
Awaited<S>
required
The initial state before the action is called. It can be any serializable value. This argument is ignored after the action is first invoked.
string
Optional string containing the unique page URL that this form modifies. For use on pages with dynamic content (e.g., feeds) in conjunction with progressive enhancement: if
action is a Server Action and the form is submitted before the JavaScript bundle loads, the browser will navigate to the specified permalink URL instead of the current page’s URL.Returns
useActionState returns an array with exactly three values:
Awaited<S>
The current state. During the first render, it matches the
initialState you passed. After the action is invoked, it matches the value returned by the action.(payload: P) => void
A dispatch function that you can pass to a form as the
action prop or call directly. When called, it will invoke the action with the current state and the payload.boolean
Whether the action is pending.
true if the action is currently executing, false otherwise.Usage
Using information from a form submission
CalluseActionState at the top level of your component to create component state that is updated when a form is submitted:
Displaying form errors
You can use the state returned byuseActionState to display error messages:
Server Actions with useActionState
With React Server Components, you can use Server Actions:Common Patterns
Form validation
Multi-step form
Optimistic updates
TypeScript
Troubleshooting
My action isn’t receiving the form data
Make sure you’re using the dispatch function returned byuseActionState as the form’s action prop: