Use useActionState and useFormStatus
Keep submission results in useActionState
useActionState(action, initialState) returns the latest action result, an action dispatcher, and a pending flag. The action receives the previous result before the submitted payload; do not treat its first argument as FormData. React processes dispatches for a Hook instance in order. The initial result should describe the idle UI. The examples require React 19+; earlier Canary documentation may call a related API useFormState.
import React,{useState,useTransition,startTransition,useActionState,useOptimistic} from 'react';import {useFormStatus} from 'react-dom';const delay=()=>new Promise(resolve=>setTimeout(resolve,600));function parseLabel(data){ const value=data.get('label'); if(typeof value!=='string') return {error:'Enter a repair label.'}; const label=value.trim(); if(!label||label.length>40) return {error:'Use 1–40 characters.'}; return {label};}const initial={message:'Ready',count:0,error:false};async function submit(previous,data){const parsed=parseLabel(data);await delay();return parsed.error?{...previous,message:parsed.error,error:true}:{message:`Queued: ${parsed.label}`,count:previous.count+1,error:false};}export default function App(){const [result,action,pending]=useActionState(submit,initial);const [label,setLabel]=useState(''); return <form action={action}><label>Repair label <input name="label" value={label} onChange={e=>setLabel(e.target.value)} disabled={pending} aria-invalid={result.error} aria-describedby="result"/></label><button type="submit" disabled={pending}>Queue</button><p role="status" id="result">{pending?'Submitting…':result.message}</p><p>Accepted submissions: {result.count}</p></form>;}Submit an empty or whitespace-only value: after the delay the message explains the error and the count stays unchanged. Submit Lamp: the count increases by one. The controlled field intentionally keeps its contents, even after success, so learners can see that the automatic form reset does not clear controlled state. The pending flag belongs to this Hook, not all forms on the page. Expected validation failures can be returned as data; unexpected thrown errors may reach an Error Boundary. If dispatching manually rather than passing it to a form action, call it in an Action/Transition.
Read the status of an ancestor form
import React,{useState,useTransition,startTransition,useActionState,useOptimistic} from 'react';import {useFormStatus} from 'react-dom';const delay=()=>new Promise(resolve=>setTimeout(resolve,600));function parseLabel(data){ const value=data.get('label'); if(typeof value!=='string') return {error:'Enter a repair label.'}; const label=value.trim(); if(!label||label.length>40) return {error:'Use 1–40 characters.'}; return {label};}function SubmitControl(){const {pending}=useFormStatus();return <button type="submit" disabled={pending}>{pending?'Sending…':'Send request'}</button>;}function RepairForm({title}){const [message,setMessage]=useState('');async function submit(data){const parsed=parseLabel(data);await delay();setMessage(parsed.error??`Received: ${parsed.label}`);} return <form action={submit}><h2>{title}</h2><label>Repair label <input name="label" required maxLength={40}/></label><SubmitControl/><p role="status">{message}</p></form>;}export default function App(){return <><RepairForm title="Electrical"/><RepairForm title="Furniture"/></>;}Submit Electrical: its nested control becomes pending while Furniture remains independently usable. useFormStatus is imported from react-dom, not react. It reads a surrounding form; calling it in RepairForm itself does not track the form that RepairForm returns. Besides pending, current React DOM exposes submission data, method and action. The data can be null when idle. Do not echo confidential field values in a status message. Disable relevant submit controls during a pending mutation, but this alone is not protection against programmatic duplicate requests or retries.
Reset result state separately from fields
import React,{useState,useTransition,startTransition,useActionState,useOptimistic} from 'react';import {useFormStatus} from 'react-dom';function parseLabel(data){ const value=data.get('label'); if(typeof value!=='string') return {error:'Enter a repair label.'}; const label=value.trim(); if(!label||label.length>40) return {error:'Use 1–40 characters.'}; return {label};}function RepairForm(){const [result,action,pending]=useActionState(async(previous,data)=>{const parsed=parseLabel(data);return parsed.error??`Accepted: ${parsed.label}`;},'Ready');return <form action={action}><label>Repair label <input name="label"/></label><button disabled={pending}>Submit</button><p role="status">{result}</p><button type="reset" disabled={pending}>Reset fields</button></form>;}export default function App(){const [session,setSession]=useState(0);return <section><button onClick={()=>setSession(v=>v+1)}>New form session</button><RepairForm key={session}/></section>;}Reset fields restores uncontrolled field defaults but does not reset useActionState. New form session replaces the component identity, clearing both the fields and local result state. This tiny mock has no lasting external work; remounting a real form does not cancel a server write. In production, coordinate session changes with pending operations and use a server-side idempotency policy where appropriate. Practice: submit, reset fields, then start a new session and explain the three different states.