Next.jsMedium
What is useFormState and useFormStatus?
useFormState was the React DOM hook for reading the result of a form action. In React 19 it was renamed to useActionState, moved to the react package, and gained an isPending value. useFormStatus (from react-dom) gives a component the pending status of the <form> it is rendered inside.
useActionState (formerly useFormState)
const [state, formAction, isPending] = useActionState(action, initialState, permalink?).action(prevState, formData)returns the next state, such as validation errors or a success message.- Pass
formActionto<form action>or<button formAction>. isPendingis true while the action runs.- Import from
react.useFormStatefromreact-domis deprecated and logs a warning.
useFormStatus
const { pending, data, method, action } = useFormStatus()fromreact-dom.- Must be called in a component inside the
<form>, not in the component that renders the form. - Ideal for reusable submit buttons and inline spinners.
Choosing between them
- Need the action's result:
useActionState. - Need only "is this form submitting" deep in the tree:
useFormStatus. - They are often combined:
useActionStatein the form component,useFormStatusin the submit button.
Related
useOptimistic for showing the expected result before the server responds, and useTransition for non-form actions.
Interview tip
Mention the rename: answering with useFormState only signals an outdated knowledge base.
Code example
tsx
1// app/feedback/form.tsx2"use client";34import { useActionState } from "react"; // was: useFormState from "react-dom"5import { useFormStatus } from "react-dom";6import { sendFeedback } from "./actions"; // (prev, formData) => { message: string }78function SubmitButton() {9 const { pending } = useFormStatus(); // reads the parent <form>'s status10 return <button disabled={pending}>{pending ? "Sending…" : "Send"}</button>;11}1213export function FeedbackForm() {14 const [state, formAction, isPending] = useActionState(sendFeedback, { message: "" });1516 return (17 <form action={formAction} aria-busy={isPending}>18 <textarea name="text" required />19 <SubmitButton />20 {state.message && <p role="status">{state.message}</p>}21 </form>22 );23}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.