React useActionState Playground — Client Form Action
Submit a form with useActionState, pending feedback and a result.
The live editor loads here with JavaScript enabled.
Run in Playground ↗The original source and explanation are available below.
Changes here stay separate from your main playground. Use Open in TryJS inside the editor to keep working with your changes.
Useful? Help keep TryJS free and independent.
Support TryJSWhat to expect
The button shows Submitting during the delay. Empty input returns a prompt; a name returns a greeting.
About this example
useActionState ties a form action to the state it returns and exposes whether it is pending. This example runs the action entirely in the browser. It is useful for trying the API before connecting it to a real backend.
How It Works
Read submitted form data
React passes FormData to the action. Reading the named input gives the value at the time of submission.
Return the next state
The string returned by the async action becomes message. pending disables the submit button and exposes progress to the user.
Read the original source
This is the starting code. Your edits stay in the live editor above.
JSX
import { useActionState } from "react";
export default function App() {
const [message, submitAction, pending] = useActionState(async (_previous, formData) => {
await new Promise(resolve => setTimeout(resolve, 800));
const name = String(formData.get("name") || "").trim();
return name ? "Hello, " + name + "!" : "Please enter a name.";
}, "Your result will appear here.");
return <main>
<h1>A form action</h1>
<form action={submitAction}>
<label>Your name <input name="name" placeholder="Ada" /></label>
<p><button disabled={pending}>{pending ? "Submitting…" : "Say hello"}</button></p>
</form>
<p aria-live="polite">{message}</p>
<small>This is a client-side action with a simulated delay.</small>
</main>;
}CSS
* { box-sizing: border-box; }
body { margin: 0; padding: 24px; background: #faf8f0; color: #222; font: 15px/1.6 system-ui, sans-serif; }
h1 { font-size: 24px; line-height: 1.2; margin: 0 0 12px; }
p { margin: 8px 0 16px; }
button, input { font: inherit; }
button { padding: 9px 16px; border: 1px solid #b9a200; border-radius: 7px; background: #f0d000; color: #222; cursor: pointer; }
button:disabled { opacity: .6; cursor: wait; }
input { max-width: 100%; padding: 9px 12px; border: 1px solid #aaa; border-radius: 7px; }
small { color: #595959; }
output { font-variant-numeric: tabular-nums; }