Capítulo 28 de 39
Controller is the component form of useController — a render-prop wrapper that connects a controlled third-party input (React-Select, AntD, MUI, react-datepicker, React Native's TextInput, …) to React Hook Form without you having to build the useController plumbing yourself.
{ field, fieldState, formState }, returning the actual input element; field carries onChange/onBlur/value/name/ref/disabled to wire onto the target component.useController — control is optional inside FormProvider.useController's props (see that chapter) — exact defaults true here too.useForm's values option can do that without Controller at all.undefined: use null or "" instead, or the input silently flips between controlled/uncontrolled and React warns.onChange: wrap field.onChange (e.g. parseInt(e.target.value)) to send a different type than the raw DOM event value, while still spreading the rest of field.<Controller
control={control}
name="ReactDatepicker"
render={({ field: { onChange, onBlur, value, ref } }) => (
<ReactDatePicker onChange={onChange} onBlur={onBlur} selected={value} />
)}
/>
ref/value contract RHF understands) via the render prop.<Controller
name="test"
render={({ field }) => (
<input {...field} onChange={(e) => field.onChange(parseInt(e.target.value))} />
)}
/>
{...field} and {...register('test')} onto the same input: double-registers it.undefined from a transformed onChange: use null/"" as the empty value instead.Controller is useController packaged as a render-prop component — pick whichever fits the calling component better.onChange reports value, onBlur reports interaction, value sets current/initial state, ref enables focus-on-error (only if the target forwards refs), name uniquely identifies the field.Controller just to sync a field with external state — useForm's values option is the lighter tool for that.values option as an alternative for simple external-state syncing.