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

Installing and Basic Setup

Setting up React Hook Form with useForm() and the register function for uncontrolled inputs.

Installing and Basic Setup

What You'll Learn

You'll learn how to install React Hook Form and wire up the useForm() hook with the register function to create uncontrolled form inputs with minimal boilerplate.

Installation

First, add React Hook Form to your project:

npm install react-hook-form

Basic Setup with useForm()

The useForm() hook is your entry point. It returns an object containing methods and state for managing your form:

import { useForm } from 'react-hook-form';

function MyForm() {
  const { register, handleSubmit } = useForm();

  const onSubmit = (data) => {
    console.log(data); // All form values in one object
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('firstName')} />
      <input {...register('email')} />
      <button type="submit">Submit</button>
    </form>
  );
}

How register() Works

The register function connects your inputs to React Hook Form. It returns an object with name, ref, onChange, and onBlur properties. Using the spread operator {...register('firstName')} applies all these props to your input at once.

Notice there's no value prop and no onChange handler you wrote yourself—this is the uncontrolled approach you learned about. React Hook Form uses refs to read values directly from the DOM when you submit.

The Magic Moment

When handleSubmit(onSubmit) runs, React Hook Form collects all registered field values and passes them as a single object to your onSubmit function. No manual state management needed.

Key Takeaway: Install react-hook-form, destructure register and handleSubmit from useForm(), then spread {...register('fieldName')} onto inputs for instant uncontrolled form management with zero re-renders during typing.