Sub topic 16
Watching values without extra re-renders
This chapter assumes you can build a form with useForm, register, and zodResolver, and that you know how React decides when a component re-renders. It covers how to read a field's current value while the user types, and how to do that without re-rendering the whole form.
Why watching costs something
React Hook Form keeps field values in the DOM inputs and in an internal store, not in React state. Typing into a registered input therefore does not re-render your component. That is the main performance benefit of the library.
When you ask for a live value, you give that up. You are telling React Hook Form to re-render a component each time the value changes. The question is which component pays that cost.
The library gives you several ways to read values, and they differ in who re-renders:
| API | Re-renders on change? | Which component re-renders | Typical use |
|---|---|---|---|
| `watch("field")` | Yes | The component that called `useForm` | Quick prototypes, small forms |
| `useWatch({ control, name })` | Yes | Only the component that calls it | Live previews, conditional fields |
| `getValues("field")` | No | None | Event handlers, one-off reads |
| `watch(callback)` | No | None | Side effects such as autosave |
watch: simple, but it re-renders the whole form
watch is called on the object returned by useForm. Because useForm lives in the form component, that component re-renders on every change to the watched field. Every child that is not memoized re-renders with it.
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const schema = z.object({
password: z.string().min(8, "Use at least 8 characters"),
});
type Values = z.infer<typeof schema>;
export function PasswordForm() {
const { register, handleSubmit, watch } = useForm<Values>({
resolver: zodResolver(schema),
defaultValues: { password: "" },
});
const password = watch("password");
console.log("PasswordForm rendered");
return (
<form onSubmit={handleSubmit((values) => console.log(values))}>
<input type="password" {...register("password")} />
<p>{password.length} / 8 characters</p>
<button type="submit">Save</button>
</form>
);
}Open the console and type. The log line prints once per keystroke. In a form with 40 fields, every one of those keystrokes re-renders all 40 field components.
useWatch: move the subscription into a child
useWatch subscribes a single component to a field. The form component does not re-render, because the subscription belongs to the child. You pass control so the child can find the form.
import { useForm, useWatch, type Control } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const schema = z.object({
password: z.string().min(8, "Use at least 8 characters"),
});
type Values = z.infer<typeof schema>;
function PasswordMeter({ control }: { control: Control<Values> }) {
const password = useWatch({ control, name: "password" });
return <p>{password.length} / 8 characters</p>;
}
export function PasswordForm() {
const { register, handleSubmit, control } = useForm<Values>({
resolver: zodResolver(schema),
defaultValues: { password: "" },
});
console.log("PasswordForm rendered");
return (
<form onSubmit={handleSubmit((values) => console.log(values))}>
<input type="password" {...register("password")} />
<PasswordMeter control={control} />
<button type="submit">Save</button>
</form>
);
}Now typing re-renders only PasswordMeter. PasswordForm logs once, on mount.
Use a default value
useWatch can return undefined on the first render if you did not set defaultValues in useForm. Set defaultValues for every field you watch. That also keeps TypeScript and runtime behavior consistent. You can pass a defaultValue option to useWatch, but the form-level default is the better place for it.
Watching several fields
Pass an array of names and you get a tuple back:
const [firstName, lastName] = useWatch({
control,
name: ["firstName", "lastName"],
});Omit name and you get the whole form's values. That re-renders on every change in any field, so use it only in a small, isolated component.
getValues: read without subscribing
If you need a value only when something happens, such as a click, you do not need a subscription at all. getValues reads the current value and never causes a render.
const { register, getValues } = useForm<Values>({
defaultValues: { password: "" },
});
const copyLength = () => {
console.log(getValues("password").length);
};getValues is not reactive. If you call it during render, the result is correct for that render only, and nothing updates it later. Use it inside handlers and effects, not to drive what is displayed.
watch with a callback: side effects without renders
watch also accepts a function. In that form it does not re-render anything. It runs your callback on every change and returns a subscription you must clean up. This suits autosave, analytics, or syncing to another store.
import { useEffect } from "react";
import { useForm } from "react-hook-form";
type DraftValues = { title: string; body: string };
export function DraftForm() {
const { register, watch } = useForm<DraftValues>({
defaultValues: { title: "", body: "" },
});
useEffect(() => {
const subscription = watch((values, { name }) => {
console.log("changed:", name);
localStorage.setItem("draft", JSON.stringify(values));
});
return () => subscription.unsubscribe();
}, [watch]);
return (
<form>
<input {...register("title")} />
<textarea {...register("body")} />
</form>
);
}The values argument is a partial object, so treat every key as possibly missing. Forgetting unsubscribe leaves a listener running after the component unmounts.
Form state subscriptions follow the same rule
formState is a proxy. React Hook Form tracks which properties you read during render and only re-renders when those change. If you read formState.isDirty, the form re-renders when dirtiness changes. If you never read isValid, validity changes cost nothing.
Two consequences follow:
- Destructure only what you use, such as
formState: { errors, isSubmitting }. - Read
formStateduring render. The proxy cannot see reads that happen later, for example inside a callback that you defined before render finished.
To isolate form state in a child component, use useFormState({ control }) the same way you use useWatch.
Validation mode adds its own re-renders
With mode: "onChange", the Zod resolver runs on every keystroke. That also updates errors and may re-render components that read it. If a form feels slow, the cause is often the validation mode and not watch. The default onSubmit mode, with reValidateMode: "onChange" after the first submit, runs far fewer validations. Measure with React DevTools Profiler before you change anything.
Choosing between them
Ask what you need the value for:
- If it drives what is shown on screen, such as a preview or a conditional field, use
useWatchin the smallest component that needs it. - If you need it once, inside a handler, use
getValues. - If you need to run code whenever it changes and it does not affect the UI, use
watch(callback)inside an effect. - If the form is tiny and you are prototyping, plain
watch("field")is acceptable. Replace it when the form grows.
Exercise
A checkout form has a country select and a large address section. You want to show a VAT notice only when country is "DE". A teammate wrote const country = watch("country") in the form component, and the whole form now re-renders on every change to any watched field. How would you fix it, and what must you pass to the new component?
Show answer
Move the notice into a small child component, for example VatNotice, and read the value there with useWatch({ control, name: "country" }). Pass control (from useForm) to the child as a prop, or get it from FormProvider with useFormContext. The form component then no longer subscribes to country, so only VatNotice re-renders when the select changes. Also make sure country has an entry in defaultValues so useWatch does not return undefined on the first render.