Course contentsShow
← React
Lesson 1217 of 2,39631. Forms in ReactFree lesson

The register Function: Connecting Inputs

How register() spreads ref, onChange, onBlur, and name props to wire inputs without manual state management.

The register Function: Connecting Inputs

What you'll learn: How React Hook Form's register() function wires up inputs without manual state management by spreading essential props automatically.

The Magic Behind register()

When you call register("fieldName"), React Hook Form returns an object containing four crucial props: ref, onChange, onBlur, and name. These props connect your input to the form library's internal tracking system.

Think of register() like plugging a device into a power strip—it provides all the necessary connections at once instead of you wiring each cable individually.

What Each Prop Does

  • ref: Gives React Hook Form direct DOM access to read the input's value
  • onChange: Tracks changes for validation and re-validation
  • onBlur: Records when the field loses focus (for "touched" state)
  • name: Identifies the field in the form data object

Using register() with Spread Syntax

const { register } = useForm();

// Spreads ref, onChange, onBlur, and name onto the input
<input {...register("email")} type="email" />

That single spread replaces this manual controlled approach:

// What you DON'T need anymore
<input
  name="email"
  value={formState.email}
  onChange={(e) => setFormState({...formState, email: e.target.value})}
  onBlur={handleBlur}
  ref={emailRef}
/>

The Uncontrolled Advantage

Notice there's no value prop—React Hook Form uses the uncontrolled pattern. The input manages its own display value while the library tracks it via the ref, avoiding re-renders on every keystroke.

Key Takeaway: register() spreads all the plumbing props needed to connect an input to React Hook Form's tracking system, eliminating manual state wiring while keeping your component performant through uncontrolled inputs.