handleSubmit: Type-Safe Form Submission
What you'll learn: How React Hook Form's handleSubmit wrapper ensures type-safe form submission while automatically preventing default browser behavior.
The Submit Handler Wrapper
In React Hook Form, you don't wire your submit function directly to the form's onSubmit event. Instead, you wrap it with handleSubmit from the useForm hook. This wrapper does two critical jobs: it prevents the default browser form submission (no page reload), and it only calls your handler if validation passes, giving you clean, validated data.
Think of handleSubmit as a security checkpoint at an airport. Your submit function is like boarding the plane—it only happens if everything checks out. The checkpoint (handleSubmit) inspects everything first, stops the normal process (default form behavior), and only lets validated passengers (data) through.
How It Works
const { register, handleSubmit } = useForm<FormData>();
const onSubmit = (data: FormData) => {
// data is already validated and type-safe!
console.log(data.email, data.password);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("email")} />
<input {...register("password")} />
<button type="submit">Submit</button>
</form>
);
Notice you're passing onSubmit into handleSubmit, not calling it yourself. React Hook Form will call preventDefault() automatically, validate all registered fields, and only invoke your onSubmit with the validated data object if everything passes.
Your onSubmit function receives fully typed, validated data—no manual event.preventDefault(), no digging into event.target, no uncertainty about data shape.
Key Takeaway: handleSubmit is your validation gatekeeper—wrap your submit handler with it to get automatic form prevention, validation enforcement, and type-safe data delivery.