Capítulo 12 de 39
getFieldState(name, formState?) (7.25.0+) returns one field's { invalid, isDirty, isTouched, isValidating, error } — a type-safe way to read a single nested field's state instead of drilling into formState.errors/dirtyFields/touchedFields by hand.
getFieldState reads from a formState subscription. If formState (or any of its properties) was already read/subscribed via useForm, useFormContext, or useFormState, the subscription already exists and the second argument is unnecessary. If no such subscription exists, pass the current formState object explicitly as the second argument, or getFieldState's result won't reflect live updates.const { register, formState: { isDirty } } = useForm()
register("test")
getFieldState("test") // ✅ — formState.isDirty was read, so a subscription exists
// without any formState read:
const { register } = useForm()
register("test")
getFieldState("test") // ❌ not subscribed, may not reflect updates
const { register, formState } = useForm()
getFieldState("test", formState) // ✅ — pass formState explicitly instead
| Return field | Requires subscription to |
|---|---|
isDirty | dirtyFields |
isTouched | touchedFields |
invalid | errors |
isValidating | isValidating |
error | errors |
getFieldState without ever reading any formState property, and without passing formState explicitly: the returned state can be stale since nothing established the subscription.getFieldState needs a live formState subscription somewhere in the tree (via a read, or via the explicit second argument) to stay accurate.getFieldState relies on.fieldState there is the same shape, scoped to one controlled field automatically.