Skip to content

Get Started

Simple form validation with React Hook Form.

Installation

Installing React Hook Form only takes a single command and you're ready to roll.

npm install react-hook-form

Example

The following code excerpt demonstrates a basic usage example:

React Web Video Tutorial

This video tutorial illustrates the basic usage and concepts of React Hook Form.

Register fields

One of the key concepts in React Hook Form is to register your component into the hook. This will make its value available for both the form validation and submission.

Note: Each field is required to have a name as a key for the registration process.

Apply validation

React Hook Form makes form validation easy by aligning with the existing HTML standard for form validation.

List of validation rules supported:

  • required

  • min

  • max

  • minLength

  • maxLength

  • pattern

  • validate

You can read more detail on each rule in the register section.

Integrating an existing form

Integrating an existing form should be simple. The important step is to register the component's ref and assign relevant props to your input.

Integrating with UI libraries

React Hook Form has made it easy to integrate with external UI component libraries. If the component doesn't expose input's ref, then you should use the Controller component, which will take care of the registration process.

Integrating Controlled Inputs

This library embraces uncontrolled components and native HTML inputs, however, it's hard to avoid working with external controlled components such as React-Select, AntD and Material-UI. To make this simple, we provide a wrapper component: Controller to streamline the integration process while still giving you the freedom to use a custom register.

Integrating with global state

It doesn't require you to rely on a state management library, but you can easily integrate with them.

Handle errors

React Hook Form provides an errors object to show you the errors in the form. errors's type will return given validation constraints. The following example showcases a required validation rule.

Schema Validation

We also support schema-based form validation with Yup, Zod , Superstruct & Joi, where you can pass your schema to useForm as an optional config. It will validate your input data against the schema and return with either errors or a valid result.

Step 1: Install Yup into your project.

npm install @hookform/resolvers yup

Step 2: Prepare your schema for validation and register inputs with React Hook Form.

React Native

You will get the same performance boost and enhancement in React Native. To integrate with input component, you can wrap it with Controller.

Expo

TypeScript

React Hook Form is built with TypeScript, and you can define a FormData type to support form values.

Want to learn more?

Check out the React Hook Form documentation and learn all about the API.

Edit