Controller for Third-Party Components
You'll learn how to integrate fancy UI libraries (like Material-UI, React Select, or date pickers) into React Hook Form's validation and state system.
The Integration Problem
React Hook Form's register() function works by attaching a ref directly to native HTML inputs. But third-party component libraries often hide the actual <input> element inside their own component logic. You can't just {...register('fieldName')} onto a Material-UI TextField or a React Select dropdown—the ref won't reach the real DOM element.
This is where Controller comes in.
What Controller Does
Think of Controller as a translator between React Hook Form's uncontrolled philosophy and controlled third-party components. It wraps the external component and handles the communication:
import { useForm, Controller } from 'react-hook-form';
import Select from 'react-select';
function MyForm() {
const { control, handleSubmit } = useForm();
return (
<form onSubmit={handleSubmit(data => console.log(data))}>
<Controller
name="favoriteColor"
control={control}
rules={{ required: "Color is required" }}
render={({ field }) => (
<Select
{...field}
options={[
{ value: 'red', label: 'Red' },
{ value: 'blue', label: 'Blue' }
]}
/>
)}
/>
<button type="submit">Submit</button>
</form>
/>
}
The control prop connects Controller to your form. The render prop receives a field object with onChange, onBlur, value, and ref—everything the third-party component needs. You spread {...field} onto the component, and Controller handles syncing its value with RHF's internal state.
Key Takeaway: Controller bridges the gap between React Hook Form's uncontrolled approach and controlled third-party components by managing value synchronization and validation integration automatically.