Sub topic 16
Watching values without extra re-renders
This chapter assumes you know useForm, register, and zodResolver from the earlier chapters, plus React's rendering rules. You will learn how to read field values while the user types without re-rendering the whole form.
Why watching costs renders
React Hook Form keeps inputs uncontrolled, so typing does not re-render your component. Subscribing to a value changes that. Each subscription re-renders whichever component made it. The only question is which component that is.
Three ways to read a value
| API | Subscribes? | Re-renders |
|---|---|---|
| `watch("field")` | Yes | The component that called `useForm` |
| `useWatch({ control, name })` | Yes | Only the component that calls it |
| `getValues("field")` | No | Nothing |
watch is the simplest, but it re-renders the entire form on every keystroke. useWatch moves that cost into a small child component. getValues reads the current value on demand, for example inside an event handler. It never triggers a render.
Isolating a watcher
In this example, the Total component is the only one that re-renders while the user types a quantity.
import { useForm, useWatch, type Control } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const orderSchema = z.object({
quantity: z.number().min(1, "At least 1"),
note: z.string(),
});
type OrderValues = z.infer<typeof orderSchema>;
function Total({ control }: { control: Control<OrderValues> }) {
const quantity = useWatch({ control, name: "quantity" });
return <p>Total: {(Number(quantity) || 0) * 5} USD</p>;
}
export function OrderForm() {
const { register, control, handleSubmit, getValues } = useForm<OrderValues>({
resolver: zodResolver(orderSchema),
defaultValues: { quantity: 1, note: "" },
});
return (
<form
onSubmit={handleSubmit((values) => console.log(values))}
noValidate
>
<input type="number" {...register("quantity", { valueAsNumber: true })} />
<input {...register("note")} />
<Total control={control} />
<button
type="button"
onClick={() => console.log("Note now:", getValues("note"))}
>
Log note
</button>
<button type="submit">Order</button>
</form>
);
}The parent passes control down. The parent itself never subscribes, so it stays quiet. The "Log note" button uses getValues because it only needs the value at click time.
Reacting without rendering
Sometimes you need a side effect, such as autosaving a draft, and not a displayed value. The callback form of watch runs on each change without re-rendering. Wrap it in an effect and clean it up.
useEffect(() => {
const subscription = watch((values, { name }) => {
console.log(name, "changed", values);
});
return () => subscription.unsubscribe();
}, [watch]);Common mistakes
- Calling
watch()with no arguments at the top of a large form. It subscribes to every field. - Reading
getValuesduring render and expecting it to update the screen. It does not subscribe. - Forgetting
controlinuseWatch, which only works without it inside aFormProvider.
Exercise
A form with 40 fields re-renders on every keystroke because its root component calls watch("country") to show a postal-code hint. How do you fix it?
Show answer
Move the hint into a child component that calls useWatch({ control, name: "country" }), and pass control from the parent. Only that child re-renders when the country changes. If the value is needed only inside a handler, use getValues("country") instead.