ReactEasy

What is React Hook Form?

React Hook Form (RHF) is a lightweight form library built on hooks and uncontrolled inputs. You register inputs, and RHF reads values from the DOM, validates them (natively or via resolvers such as Zod), and tracks errors and submission state while re-rendering only what is needed. It is the most widely used React form library today (version 7).

Core API

  • const { register, handleSubmit, formState, reset, watch, setValue, control } = useForm<FormType>(options).
  • register("email", { required: "Email is required" }) returns name, ref, onChange and onBlur to spread on an input.
  • handleSubmit(onValid, onInvalid) validates, then calls your handler with typed data.
  • formState: errors, isSubmitting, isDirty, isValid, touchedFields. It is a proxy, so components only re-render for the fields they read.
  • resolver: zodResolver(schema) for schema validation.
  • useController / <Controller> for controlled third-party components (date pickers, selects).
  • useFieldArray for dynamic lists of fields.
  • useWatch to subscribe to specific values without re-rendering the whole form.
  • mode: onSubmit (default), onBlur, onChange, onTouched, all.

Why it is fast

Inputs are uncontrolled, so typing does not re-render the form component. Validation and error display update only affected parts.

With React 19 and Server Functions

RHF handles client-side UX; you can still send the data to a Server Function and map returned server errors back with setError.

Code example

tsx
1import { useFieldArray, useForm } from "react-hook-form";
2
3type Invoice = { customer: string; items: { description: string; amount: number }[] };
4
5export function InvoiceForm() {
6 const { register, control, handleSubmit, formState: { errors, isSubmitting }, setError } = useForm<Invoice>({
7 defaultValues: { customer: "", items: [{ description: "", amount: 0 }] },
8 });
9 const { fields, append, remove } = useFieldArray({ control, name: "items" });
10
11 async function onSubmit(data: Invoice) {
12 const res = await fetch("/api/invoices", { method: "POST", body: JSON.stringify(data) });
13 if (res.status === 409) setError("customer", { message: "Customer is archived" }); // server error -> field
14 }
15
16 return (
17 <form onSubmit={handleSubmit(onSubmit)}>
18 <input {...register("customer", { required: "Customer is required" })} placeholder="Customer" />
19 {errors.customer && <p role="alert">{errors.customer.message}</p>}
20
21 {fields.map((field, i) => (
22 <div key={field.id}>
23 <input {...register(`items.${i}.description`, { required: true })} />
24 <input type="number" step="0.01" {...register(`items.${i}.amount`, { valueAsNumber: true, min: 0 })} />
25 <button type="button" onClick={() => remove(i)}>Remove</button>
26 </div>
27 ))}
28 <button type="button" onClick={() => append({ description: "", amount: 0 })}>Add line</button>
29 <button disabled={isSubmitting}>Save</button>
30 </form>
31 );
32}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app