# React Hook Form & Zod

> The modern industry standard for handling complex forms in React, prioritizing performance by minimizing re-renders and using schema validation.

Subject: React | Lesson 40 of 48 | 6 min read
Interactive lesson: https://codewithanimation.in/react/react-hook-form
Previous: [Controlled Inputs & Validation](https://codewithanimation.in/react/controlled-inputs-validation.md) | Next: [TypeScript in React](https://codewithanimation.in/react/typescript-in-react.md)

## The Performance Problem with Controlled Forms

As you learned, controlled inputs trigger a React state update on every single keystroke. If you have a massive form with 50 fields, typing a single letter causes all 50 fields to re-render.

**React Hook Form (RHF)** solves this by using *uncontrolled* inputs under the hood. It uses `useRef` to track the input values silently, only triggering a re-render when absolutely necessary (like when an error occurs or the form is submitted).

## Schema Validation with Zod

Writing manual `if (password.length < 6)` logic doesn't scale for complex enterprise forms.

RHF pairs perfectly with **Zod**, a TypeScript-first schema declaration library. You define the "shape" of your valid data once (e.g., `z.object({ email: z.string().email() })`).

RHF runs the Zod schema automatically on submission or change, and extracts the exact error messages to display in the UI, keeping your component code incredibly clean.

## Registering Inputs

Instead of writing `value={state} onChange={handleChange}`, you use RHF's `register` function.

`const { register, handleSubmit, formState: { errors } } = useForm();`

You simply spread the register function onto your input: `<input {...register("firstName")} />`.

This wires up the ref and the event listeners automatically, completely bypassing the normal React render cycle while the user types.

## Common mistake: Integration with UI Libraries

Because RHF relies on raw DOM refs, it can sometimes be tricky to use with custom UI component libraries (like Material UI) that don't expose the underlying `<input>` ref directly.

To solve this, RHF provides a `<Controller>` component that bridges the gap, allowing you to use controlled third-party components while still getting the performance benefits of RHF's central state.

## Tip: Watch only what you need

Sometimes you *do* need to re-render based on a specific input (e.g., showing a 'Shipping Address' section only if 'Deliver to Door' is checked).

You can use RHF's `watch('fieldName')` function to selectively subscribe to just that one field, keeping the rest of the form optimized.

## Try it: The Re-render Storm

Compare a standard controlled form to React Hook Form. Type into the fields and watch the visual indicator flashes. Notice how the controlled form re-renders everything, while RHF only updates the isolated input.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/react-hook-form

## Check yourself

### Question 1

How does React Hook Form improve performance over traditional controlled components?

- A. It uses a Web Worker to handle form state.
- B. It uses uncontrolled inputs via `useRef`, avoiding full component re-renders on every keystroke.
- C. It debounces the `onChange` events automatically.

Correct answer: B

RHF bypasses the React render cycle during typing by tracking values in refs, drastically reducing unnecessary renders in large forms.

### Question 2

What is the primary role of Zod when used with React Hook Form?

- A. To format the date and time strings.
- B. To define the data schema and automatically validate the form inputs against it.
- C. To send the form data to the backend API.

Correct answer: B

Zod acts as the rulebook. It defines what valid data looks like, and RHF uses it to automatically block submission and generate error messages if the rules are broken.

### Question 3

Instead of manually providing `value` and `onChange`, how do you connect an `<input>` to React Hook Form?

- A. By spreading the `register` function: `<input {...register('fieldName')} />`
- B. By passing the state setter: `<input onChange={setForm} />`
- C. By using the `<FormInput>` wrapper component.

Correct answer: A

The `register` function returns an object containing `onChange`, `onBlur`, `name`, and `ref`. By spreading it (`...`), you apply all these props to the input element simultaneously.

## Remember this

- Traditional controlled forms re-render the entire component on every keystroke.
- React Hook Form uses uncontrolled inputs (refs) to eliminate typing re-renders.
- Use the `register` function to connect native inputs to the form state.
- Use Zod to declare your validation rules (schemas) cleanly outside your component.
- Use `watch()` if you explicitly need the component to re-render when a specific field changes.
