useActionState

The React hook that wraps an Action and returns its latest result, a function to trigger it, and an isPending flag.

In more detail

useActionState(action, initialState) gives [state, formAction, isPending]; pass formAction to a form action prop. Each call receives the previous state and the submitted FormData, and whatever it returns becomes the new state, such as a success or error message.

Examples

const [state, formAction, isPending] = useActionState(updateProfile, { errors: {} });
const [state, formAction, isPending] = useActionState(saveAction, null);

Related terms

Taught in

References