Model pending submissions with Actions
An Action represents a pending update
In React 19, an Action is a function that runs within a Transition, often performing asynchronous work and updating the interface when it finishes. React can track the pending interval rather than requiring a separate Boolean for every request. It is not necessarily a server operation. These examples use matching patched React and React DOM 19+ in an approved client app, with local mock work only. Expected screen behavior has not been browser-tested.
import React,{useState,useTransition,startTransition,useActionState,useOptimistic} from 'react';import {useFormStatus} from 'react-dom';const delay=()=>new Promise(resolve=>setTimeout(resolve,600));export default function App(){const [checked,setChecked]=useState(false);const [pending,run]=useTransition(); return <section><button disabled={pending||checked} onClick={()=>run(async()=>{await delay();startTransition(()=>setChecked(true));})}>Check request</button><p role="status">{pending?'Checking…':checked?'Checked':'Not checked'}</p></section>;}The button starts an asynchronous Transition. During the mock delay, the status can say Checking and the button is disabled. After completion, Checked replaces it. The nested startTransition marks the state update after await as non-urgent under the documented async-context limitation. This is not a background thread, nor does it guarantee exact render timing. Do not put an urgent controlled input update in a Transition.
Submit a form with a function action
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};}export default function App(){const [result,setResult]=useState(''); async function submit(data){const parsed=parseLabel(data);setResult(parsed.error??`Received: ${parsed.label}`);} return <form action={submit}><label>Repair label <input name="label" required maxLength={40}/></label><button type="submit">Send</button><p role="status">{result}</p></form>;}The form sends a FormData object to the client function. Enter Lamp and submit: Received: Lamp appears. A name is needed for an ordinary text input to contribute a field; disabled controls are generally excluded. FormData.get returns the first matching value or null; file inputs can yield File objects, so validate the type. The required attribute helps the user but is not a security guarantee. React resets uncontrolled inputs after a successful function Action; returning an ordinary validation error value is not the same as throwing. If preserving text on validation failure is important, own it in controlled state as shown in the next lesson. A function action uses POST semantics even when method specifies otherwise. This local function does not create an HTTP endpoint.
Choose an action with a submit button
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};}export default function App(){const [label,setLabel]=useState('');const [message,setMessage]=useState(''); function handle(data,verb){const parsed=parseLabel(data);if(parsed.error){setMessage(parsed.error);return;}setMessage(`${verb}: ${parsed.label}`);if(verb==='Queued')setLabel('');} return <form action={data=>handle(data,'Queued')}><label>Repair label <input name="label" value={label} onChange={e=>setLabel(e.target.value)} required maxLength={40}/></label><button type="submit">Queue</button><button type="submit" formAction={data=>handle(data,'Preview')}>Preview</button><p role="status">{message}</p></form>;}Preview shows the label and retains the controlled text. Queue displays the confirmation and explicitly clears it. The selected submitter can override the default form action; use the primary submit button first for the intended default keyboard behavior. Neither action writes to a real queue. A Server Function is a separate framework-integrated server execution mechanism with authentication and validation responsibilities. React 18 alternatives use onSubmit, preventDefault, and explicit pending/error state. Practice: compare preview and queue, explain how controlled reset differs from uncontrolled reset, and identify which data is sent.