_form_ – React
_form_ – React
v19
<form>
The built-in browser <form> component lets you create
interactive controls for submitting information.
<form action={search}>
<input name="query" />
<button type="submit">Search</button>
</form>
Reference
<form>
Usage
Reference
<form>
https://react.dev/reference/react-dom/components/form 1/11
20/02/2025, 10:01 <form> – React
<form action={search}>
<input name="query" />
<button type="submit">Search</button>
</form>
Props
action : a URL or function. When a URL is passed to action the form will
action may be async and will be called with a single argument containing
the form data of the submitted form. The action prop can be overridden by
a formAction attribute on a <button> , <input type="submit"> , or <input
type="image"> component.
Caveats
Usage
Pass a function to the action prop of form to run the function when the
form is submitted. formData will be passed to the function as an argument
so you can access the data submitted by the form. This differs from the
conventional HTML action, which only accepts URLs. After the action
function succeeds, all uncontrolled field elements in the form are reset.
https://react.dev/reference/react-dom/components/form 2/11
20/02/2025, 10:01 <form> – React
Render a <form> with an input and submit button. Pass a Server Function (a
function marked with 'use server' ) to the action prop of form to run the
function when the form is submitted.
You can use hidden form fields to provide data to the <form> ’s action. The
Server Function will be called with the hidden form field data as an instance
of FormData .
function AddToCart({productId}) {
async function addToCart(formData) {
'use server'
const productId = formData.get('productId')
await updateCart(productId)
}
return (
<form action={addToCart}>
<input type="hidden" name="productId" value={productId} />
<button type="submit">Add to Cart</button>
</form>
);
}
In lieu of using hidden form fields to provide data to the <form> ’s action, you
can call the bind method to supply it with extra arguments. This will bind a
new argument ( productId ) to the function in addition to the formData
that is passed as an argument to the function.
function AddToCart({productId}) {
async function addToCart( productId , formData ) {
"use server";
await updateCart(productId)
}
const addProductToCart = addToCart. bind (null, productId );
return (
<form action={addProductToCart}>
<button type="submit">Add to Cart</button>
</form>
https://react.dev/reference/react-dom/components/form 4/11
20/02/2025, 10:01 <form> – React
);
}
To display a pending state when a form is being submitted, you can call the
useFormStatus Hook in a component rendered in a <form> and read the
App.js Reset
function Submit() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Submitting..." : "Submit"}
</button>
);
}
Show more
https://react.dev/reference/react-dom/components/form 5/11
20/02/2025, 10:01 <form> – React
For example, when a user types a message into the form and hits the “Send”
button, the useOptimistic Hook allows the message to immediately appear
in the list with a “Sending…” label, even before the message is actually sent
to a server. This “optimistic” approach gives the impression of speed and
responsiveness. The form then attempts to truly send the message in the
background. Once the server confirms the message has been received, the
“Sending…” label is removed.
https://react.dev/reference/react-dom/components/form 6/11
20/02/2025, 10:01 <form> – React
await sendMessage(formData);
}
Show more
App.js Reset
https://react.dev/reference/react-dom/components/form 7/11
20/02/2025, 10:01 <form> – React
Show more
state. useActionState returns two values, a state variable and an action. The
action returned by useActionState should be passed to the action prop of
the form. The state variable returned by useActionState can be used to
display an error message. The value returned by the Server Function passed
to useActionState will be used to update the state variable.
https://react.dev/reference/react-dom/components/form 8/11
20/02/2025, 10:01 <form> – React
App.js Reset
Show more
Learn more about updating state from a form action with the
useActionState docs
function save(formData) {
const content = formData.get("content");
alert(`Your draft of '${content}' has been saved!`);
}
Show more
https://react.dev/reference/react-dom/components/form 10/11
20/02/2025, 10:01 <form> – React
PREVIOUS
NEXT
<input>
uwu?
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/reference/react-dom/components/form 11/11