Skip to content

Build UINot attempted

Form with useActionState

React + TSDifficulty: easy

Build a name + email form using React 19's <form action> and useActionState, with a pending state while it "submits" (simulate 1s latency).

Preview: React 19 with Tailwind v4 (including tokens like bg-surface and text-text-muted); bare npm imports such as lucide-react load from esm.sh. Add .ts / .tsx files in the file tree and import them relatively; App.tsx's default export is what renders.

Requirements · 0 / 4

Tick every requirement after checking it in the preview.
Show reference solution
import { useActionState } from "react";
type FormState = {
name: string;
email: string;
submittedAt: string | null;
};
const initialState: FormState = { name: "", email: "", submittedAt: null };
async function submitAction(_prev: FormState, formData: FormData): Promise<FormState> {
const name = String(formData.get("name") ?? "");
const email = String(formData.get("email") ?? "");
// Simulate a network request.
await new Promise((resolve) => setTimeout(resolve, 1000));
return { name, email, submittedAt: new Date().toLocaleTimeString() };
}
export default function MyForm() {
const [state, action, pending] = useActionState(submitAction, initialState);
return (
<div className="mx-auto max-w-sm space-y-6 p-6">
<form action={action} className="space-y-4">
<div className="space-y-1">
<label htmlFor="af-name" className="text-sm font-medium text-text-main">Name</label>
<input
id="af-name"
type="text"
name="name"
required
defaultValue={state.name}
className="w-full rounded-lg border border-border-subtle bg-surface px-3 py-2"
/>
</div>
<div className="space-y-1">
<label htmlFor="af-email" className="text-sm font-medium text-text-main">Email</label>
<input
id="af-email"
type="email"
name="email"
required
defaultValue={state.email}
className="w-full rounded-lg border border-border-subtle bg-surface px-3 py-2"
/>
</div>
<button
type="submit"
disabled={pending}
className="w-full rounded-lg bg-brand-500 py-2.5 font-semibold text-white transition-colors hover:bg-brand-600 disabled:cursor-not-allowed disabled:opacity-60"
>
{pending ? "Submitting…" : "Submit"}
</button>
</form>
{state.submittedAt && (
<div className="rounded-lg border border-border-subtle bg-muted p-4 text-sm" aria-live="polite">
<p className="font-semibold text-text-main">Submitted at {state.submittedAt}</p>
<p className="text-text-muted">Name: {state.name}</p>
<p className="text-text-muted">Email: {state.email}</p>
</div>
)}
</div>
);
}
Loading editor…