| title | handleSubmit |
|---|---|
| description | Ready to send to the server |
| sidebar | apiLinks |
</> handleSubmit: ((data: Object, e?: Event) => Promise<void>, (errors: Object, e?: Event) => Promise<void>) => Promise<void>
This function will receive the form data if form validation is successful.
| Name | Type | Description |
|---|---|---|
| SubmitHandler | (data: Object, e?: Event) => Promise<void> |
A successful callback. |
| SubmitErrorHandler | (errors: Object, e?: Event) => Promise<void> |
An error callback. |
-
You can easily submit form asynchronously with handleSubmit.
handleSubmit(onSubmit)() // You can pass an async function for asynchronous validation. handleSubmit(async (data) => await fetchAPI(data))
-
disabledinputs will appear asundefinedvalues in form values. If you want to prevent users from updating an input and wish to retain the form value, you can usereadOnlyor disable the entire <fieldset />. Here is an example. -
handleSubmitfunction will not swallow errors that occurred inside your onSubmit callback, so we recommend you to try and catch inside async request and handle those errors gracefully for your customers.const onSubmit = async () => { // async request which may result error try { // await fetch() } catch (e) { // handle your error setError("service", { type: "custom", message: "something went wrong", }) } } return <form onSubmit={handleSubmit(onSubmit)} />
Examples:
Sync
<TabGroup buttonLabels={["TS", "JS"]}>
import { useForm, SubmitHandler, SubmitErrorHandler } from "react-hook-form"
type FormValues = {
firstName: string
lastName: string
email: string
}
export default function App() {
const { register, handleSubmit } = useForm<FormValues>()
const onSubmit: SubmitHandler<FormValues> = (data) => console.log(data)
const onError: SubmitErrorHandler<FormValues> = (errors) =>
console.log(errors)
return (
<form onSubmit={handleSubmit(onSubmit, onError)}>
<input {...register("firstName")} />
<input {...register("lastName")} />
<input type="email" {...register("email")} />
<input type="submit" />
</form>
)
}import { useForm } from "react-hook-form"
export default function App() {
const { register, handleSubmit } = useForm()
const onSubmit = (data, e) => console.log(data, e)
const onError = (errors, e) => console.log(errors, e)
return (
<form onSubmit={handleSubmit(onSubmit, onError)}>
<input {...register("firstName")} />
<input {...register("lastName")} />
<button type="submit">Submit</button>
</form>
)
}Async
import { useForm } from "react-hook-form";
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
function App() {
const { register, handleSubmit, formState: { errors } } = useForm();
const onSubmit = async data => {
await sleep(2000);
if (data.username === "bill") {
alert(JSON.stringify(data));
} else {
alert("There is an error");
}
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label htmlFor="username">User Name</label>
<input placeholder="Bill" {...register("username"} />
<input type="submit" />
</form>
);
}The following video tutorial explains the handleSubmit API in detail.