Capítulo 35 de 39
useWatch({ control, name?, compute?, defaultValue?, exact? }) is watch's isolated-re-render sibling — call it inside a small child component and only that component re-renders when the watched value changes, instead of the whole form-holding component.
'' unless a condition is met).false — a subscription to "users" also fires on changes to "users.0.name" (prefix match); set true to require an exact name match.useWatch only sees updates that happen after its subscription is set up — calling setValue before a given useWatch call runs means that specific update is missed by that watcher, even though the underlying value did change. A useFormValues custom hook that spreads ...useWatch() then ...getValues() (getValues last, so it overrides with the latest) sidesteps this for a "give me the freshest snapshot" use case.useEffect dependencies: useWatch's return is optimized for the render phase; use a separate value-comparison hook if you need to react to changes outside render.function FirstNameWatched({ control }) {
const firstName = useWatch({ control, name: "firstName", defaultValue: "default" })
return <p>Watch: {firstName}</p> // only this component re-renders on change
}
export const Calc = ({ control, setValue }) => {
const results = useWatch({ control, name: "test" }) // "test" is a field array
const output = totalCal(results) // sum quantities/prices recursively
setValue("total", output)
return <p>{output}</p>
}
useFieldArray + useWatch pairing.| Call | Return type |
|---|---|
useWatch({ name: 'field' }) | unknown |
useWatch({ name: ['a','b'] }) | unknown[] |
useWatch() | { [key: string]: unknown } |
setValue before the relevant useWatch subscription exists: that specific update is silently missed by that watcher — order matters.useWatch's return value inside a useEffect dependency array expecting change detection semantics: it's render-phase optimized, not effect-phase; use a dedicated comparison hook instead.useWatch() with no name) in a large form just to read one field: defeats the isolation purpose — scope name (and compute) as tightly as possible.useWatch == watch, but scoped to its own hook/component for re-render isolation — prefer it over watch in components that don't need the full form.compute (7.61.0+) lets you subscribe to a derived value instead of the raw field, reducing re-renders even further.useWatch is the standard way to compute totals/derived state from array-field values.<Watch> component built on this hook.