Sub topic 13
Using controlled components with Controller
This chapter assumes you know register, handleSubmit, and zodResolver from the earlier chapters, and that you can read a basic React component with props.
Why register is not always enough
register works by attaching a ref to a native input. React Hook Form then reads the value straight from the DOM. Many UI components don't expose a ref to a native input. Custom selects, date pickers, rating widgets, and most component-library inputs instead take a value prop and an onChange callback. These are controlled components, so register can't connect to them.
Controller closes that gap. It subscribes one field to the form and passes you the pieces a controlled component needs.
The render prop
Controller takes name and control, plus a render function. The function receives an object with two parts:
fieldhasvalue,onChange,onBlur,name, andref.fieldStatehaserror,invalid,isTouched, andisDirtyfor that field.
You wire field into your component and read fieldState.error to show the message from your Zod schema.
A worked example
This form uses a custom rating widget built from buttons. The schema requires a rating of at least 1, and the default of 0 means "nothing chosen yet".
import { useForm, Controller } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const feedbackSchema = z.object({
rating: z.number().min(1, "Pick a rating").max(5),
});
type FeedbackValues = z.infer<typeof feedbackSchema>;
type RatingInputProps = {
value: number;
onChange: (value: number) => void;
onBlur: () => void;
};
function RatingInput({ value, onChange, onBlur }: RatingInputProps) {
return (
<div role="radiogroup" aria-label="Rating">
{[1, 2, 3, 4, 5].map((n) => (
<button
key={n}
type="button"
role="radio"
aria-checked={value === n}
onClick={() => onChange(n)}
onBlur={onBlur}
>
{n}
</button>
))}
</div>
);
}
export function FeedbackForm() {
const { control, handleSubmit } = useForm<FeedbackValues>({
resolver: zodResolver(feedbackSchema),
defaultValues: { rating: 0 },
});
return (
<form onSubmit={handleSubmit((values) => console.log(values))}>
<Controller
name="rating"
control={control}
render={({ field, fieldState }) => (
<>
<RatingInput
value={field.value}
onChange={field.onChange}
onBlur={field.onBlur}
/>
{fieldState.error && (
<p role="alert">{fieldState.error.message}</p>
)}
</>
)}
/>
<button type="submit">Send</button>
</form>
);
}Clicking a button calls field.onChange(n), which stores the number in form state. On submit, zodResolver checks the value, and a failure appears in fieldState.error. The form never reads a DOM node here.
Choosing between register and Controller
Use register for native input, select, and textarea elements. Use Controller only when a component needs value and onChange props. Native inputs are cheaper because they stay uncontrolled.
Exercise
A checkbox from a component library takes checked and onCheckedChange props. Which field properties do you pass, and what must the schema field and default value be?
Show answer
Pass checked={field.value} and onCheckedChange={field.onChange}. The schema field should be z.boolean(), and defaultValues should include a boolean such as false.