useActionState
ELI5 - The Vibe Check
useActionState is React's way of handling I submitted a form, now what, without a pile of useState calls for loading, errors, and results. You hand it an action function and it hands back the latest result plus a pending flag, already wired up to a form. Less plumbing, same form.
Real Talk
useActionState is a React hook that manages the state of a form action, taking an action function and initial state and returning the current state, a wrapped action to pass to a form, and a pending boolean. It replaces the earlier useFormState API and works with both client functions and Server Actions.
Show Me The Code
function ChangeName({ name }) {
const [state, formAction, pending] = useActionState(
async (prevState, formData) => {
const newName = formData.get('name');
return await updateName(newName);
},
{ name }
);
return (
<form action={formAction}>
<input name="name" defaultValue={state.name} />
<button disabled={pending}>Save</button>
</form>
);
}
When You'll Hear This
"useActionState gives us the pending flag for free, so we dropped the isSubmitting state." / "Swap useFormState for useActionState, the newer React version renamed it."
Related Terms
React
React is a JavaScript library from Meta for building UIs out of components.
React Hook Form
React Hook Form is the form library that said 'what if we just... didn't re-render the whole form on every keystroke?
Server Actions
Server Actions let you write a function in your React component that magically runs on the server when called.
useOptimistic
useOptimistic lets your UI lie a little, in a good way.