Sub topic 3
Handling submission with handleSubmit
This chapter assumes you know React function components and hooks, basic TypeScript generics, the HTML form submit event, and async/await. It also assumes you have used register from the previous chapter.
What handleSubmit does
handleSubmit is a function returned by useForm. You pass it your own callback, and it returns the function you give to the form's onSubmit. When the user submits, it does four things in order:
- It calls
event.preventDefault(), so the browser does not reload the page. - It reads the current values of all registered inputs.
- It runs validation.
- It calls your success callback if validation passes. If validation fails, it calls your optional error callback.
A complete example
import {
useForm,
type SubmitHandler,
type SubmitErrorHandler,
} from "react-hook-form";
type LoginValues = { email: string; password: string };
export function LoginForm() {
const {
register,
handleSubmit,
formState: { isSubmitting },
} = useForm<LoginValues>({
defaultValues: { email: "", password: "" },
});
const onValid: SubmitHandler<LoginValues> = async (values) => {
await fetch("/api/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(values),
});
};
const onInvalid: SubmitErrorHandler<LoginValues> = (errors) => {
console.log("Invalid fields:", Object.keys(errors));
};
return (
<form onSubmit={handleSubmit(onValid, onInvalid)} noValidate>
<input type="email" {...register("email", { required: "Email is required" })} />
<input
type="password"
{...register("password", { required: "Password is required" })}
/>
<button type="submit" disabled={isSubmitting}>
Log in
</button>
</form>
);
}The register rules here are React Hook Form's built-in ones. Later chapters replace them with a Zod schema. handleSubmit works the same either way.
Async callbacks and isSubmitting
If your success callback returns a promise, handleSubmit waits for it. While it is pending, formState.isSubmitting is true. Use it to disable the button and block double submits. It returns to false when the promise settles.
Common errors
- Passing the callback directly. Writing
onSubmit={onValid}skips validation andpreventDefault. Always writeonSubmit={handleSubmit(onValid)}. - Calling it inline. Writing
onSubmit={handleSubmit(onValid)()}runs it during render. - Forgetting
noValidate. Without it, the browser's own validation bubbles can appear before React Hook Form runs. - Expecting the success callback to run on invalid data. It never does. Use the second argument to react to failures.
- Throwing inside the callback.
handleSubmitrethrows the error, so catch failures from your API call yourself.
Exercise
A teammate wrote <form onSubmit={onValid}> and says validation seems to be ignored and the page reloads. What is wrong, and what is the fix?
Show answer
onValid is attached directly, so handleSubmit never runs. That means there is no preventDefault call and no validation. The fix is <form onSubmit={handleSubmit(onValid)}>.