Capítulo 31 de 39
useFormContext() retrieves the full useForm() return value from React Context, avoiding prop-drilling in deeply nested form trees; it requires the tree to be wrapped in FormProvider.
UseFormReturn: same shape as calling useForm() directly.useFormContext only works inside a tree wrapped in <FormProvider {...methods}>.useFormContext<TFieldValues, TContext, TTransformedValues>() types a resolver's transformed output separately (since 7.44.0).useFormState for subscribing to state values: formState from useFormContext() is wrapped in a Proxy that only tracks fields actually read during render; destructuring it and reading a property later (in a callback) or conditionally won't register a subscription, so the component silently won't re-render on change — use useFormState when you need to react to errors/isDirty/dirtyFields.export default function App() {
const methods = useForm()
const { register, reset } = methods
useEffect(() => { reset({ name: "data" }) }, [reset]) // never depend on `methods` itself
return (
<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(onSubmit)}>
<NestedInput />
</form>
</FormProvider>
)
}
function NestedInput() {
const { register } = useFormContext()
return <input {...register("test")} />
}
formState from useFormContext() and reading a property outside render (in a callback, or behind a condition): breaks the Proxy-based subscription tracking — use useFormState instead when the read isn't unconditional and at render time.useFormContext is the read side of the FormProvider pattern — always pair them.formState value read conditionally or inside a callback, use useFormState, not useFormContext().formState.formState slices with proper Proxy tracking.