Capítulo 6 de 39
useForm(options?) is the entry point of the library — one hook call that returns every method (register, handleSubmit, control, watch, formState, …) needed to build, validate, and submit a form; its options object controls validation timing, default/reactive values, schema integration, and global behaviors like disabling or unregistering.
onSubmit (default, validates on submit then re-validates changed fields on change), onBlur, onChange (most re-renders), onTouched (first blur, then every change), or all (blur + change).onChange. Both mode and reValidateMode are reactive since 7.56.0 — updating them after init takes effect on subsequent validations.() => Promise<FieldValues>); cached, included in submission by default, reset only via reset(). Avoid undefined values and prototype-heavy objects (Moment/Luxon).defaultValues on change (server data, external state) unless resetOptions.keepDefaultValues is set.KeepStateOptions applied automatically when values/defaultValues update asynchronously (e.g. keepDirtyValues, keepErrors).resolver's second argument (or Yup's validation context).firstError (default, one error per field) or all (collect every rule violation per field).true) — only works if that field's ref is attached to a real DOM element.({formValues, formState, eventType, name}) => true | string | { field: {type, message} }; mutually exclusive with resolver (resolver wins if both set).false by default (unmounted inputs keep their value and are skipped by built-in validation). Set to true for form behavior closer to native HTML forms — global setting only, not overridable at instance level for individual inputs; unmounted-input notification for shouldUnregister: true requires either registering inside useForm's own effect or reading the toggling condition via useWatch, not through a prop passed to a child that never re-renders.setCustomValidity/reportValidity) from RHF's own results; only with onSubmit/onChange modes and register/Controller-connected DOM refs; independent of progressive.required, min, max, minLength, maxLength, pattern) as real HTML attributes, enabling usable forms before hydration.createFormControl instead of letting useForm create its own internally.rules; cannot be combined with built-in validators on the same field; the returned errors object must be hierarchical, matching nested field paths ({ participants: [null, { name: err }] }), not dot-notation flat keys.import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import * as z from "zod"
const schema = z.object({ name: z.string(), age: z.number() })
const { register, handleSubmit } = useForm({
resolver: zodResolver(schema),
})
rules for a schema resolver — the most common useForm configuration beyond the defaults.methods object (from const methods = useForm()) in a useEffect dependency array: a future release memoizes the return so it changes reference on every formState update — depend on the specific destructured method (methods.reset, or better, const { reset } = useForm()) instead.errors prop: causes infinite re-renders since it's treated as a reactive update source.resolver and validate: validate silently never runs.shouldUnregister-conditioned mount state from a plain prop instead of useWatch: the child never gets notified of the unmount, so RHF can't verify the input actually left the DOM.mode/reValidateMode control when validation runs, independently for before- and after-first-submit.resolver and validate are mutually exclusive; resolver always wins if both are configured.shouldUseNativeValidation and progressive are independent knobs — combine them if you want both the Constraint Validation API behavior and the raw HTML attributes present on the DOM.useForm() returns 19 named members (register, unregister, formState, watch, subscribe, handleSubmit, reset, resetField, resetDefaultValues, setError, clearErrors, setValue, setValues, setFocus, getValues, getErrors, getFieldState, trigger, control, Form) — each documented as its own chapter in this skill.formControl option.control value this hook returns.