DEV Community

Kazutora Hattori
Kazutora Hattori

Posted on

React Hook Form Summary: How to Easily Manage Forms Even for Beginners

What is React Hook Form

・A library that simplifies form management and creation
・Creating forms normally involves tedious tasks like “input value management” and “validation processing,” but React Hook Form automates the following:

Input Value Management (No State Management Required)

Easy Configuration of Input Checks (Validation)

Centralized Error Management

Core Functions & Objects

useForm

Provides all necessary elements for form management.

Example:

const { register, handleSubmit, reset, formState: { errors }, watch } = useForm<FormData>();
Enter fullscreen mode Exit fullscreen mode

register

Adds input fields to the form. Monitors input values and errors.

handleSubmit

Called when the form is submitted.

If input validation passes → Executes the specified function (e.g., onSubmit)

If fails → Information is stored in errors

errors

A “container” that stores errors when input mistakes occur.
Holds error information for each field name.

reset

Resets the form contents.

watch

Allows real-time monitoring of input values.

Example

<input
  {...register(“name”, {
    required: “Name is required.”,
    maxLength: { value: 20, message: “Please enter a name within 20 characters.” }
  })}
/>
{errors.name && <p>{errors.name.message}</p>}
Enter fullscreen mode Exit fullscreen mode

“name”: The name of this input field (value retrievable later as data.name)

required: Triggers an error if left empty

maxLength: Triggers an error if exceeds specified character count

Actual Behavior

Submit without entering anything → “Name is required.”

Submit with 30 characters entered → “Please keep your name within 20 characters.”

Submit with correct input → OK

Why Controllers Are Needed

For “complex components” composed of multiple internal elements, like Chakra UI's NumberInput or MUI's Checkbox, register cannot be used directly.

In such cases, use a Controller to “mediate between React Hook Form and the UI”.

Summary

Using form tags automatically collects input values upon submission

register → Turns input fields into “special form inputs” (watch + apply rules)

handleSubmit → Checks on submission; executes function if OK / throws error if NG

errors → Container for error information

Controller → Required for complex UI

watch → Displays input values in real time

Translated with DeepL.com (free version)

Top comments (0)