DEV Community

Cover image for An Easy Guide to Building a Basic Form with React Hook Form.
Manav Shete
Manav Shete

Posted on • Edited on

An Easy Guide to Building a Basic Form with React Hook Form.

React Hook Form is a powerful library for managing form state and validation in React applications using hooks. In this tutorial, we will explore how to set up a basic form using React Hook Form. We'll cover installation, creating a form, form validation, and submitting form data.

Table of Contents

  1. Introduction to React Hook Form
  2. Installation
  3. Creating the Form Component
  4. Form Input Fields and Validation
  5. Handling Form Submission

1. Introduction to React Hook Form

React Hook Form is a library that simplifies form handling in React applications by utilizing hooks. It provides a straightforward way to manage form state, handle form validation, and streamline form submission.

2. Installation

npm install react-hook-form

3.Creating the Form Component

Let's create a new component for our form. For this example, we'll create a SimpleForm.js file in your project's src folder.

// src/SimpleForm.js
import React from 'react';
import { useForm } from 'react-hook-form';

const SimpleForm = () => {
  const { register, handleSubmit, formState: { errors } } = useForm();

  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      {/* Form fields will be added here */}
    </form>
  );
};

export default SimpleForm;

Enter fullscreen mode Exit fullscreen mode

4. Form Input Fields and Validation

Let's add some input fields to our form along with basic validation. We'll create fields for the user's name and email address. We'll use the register function to associate input fields with React Hook Form.

// src/SimpleForm.js
import React from 'react';
import { useForm } from 'react-hook-form';

const SimpleForm = () => {
  const { register, handleSubmit, formState: { errors } } = useForm();

  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <div>
        <label>Name</label>
        <input
          type="text"
          {...register('name', { required: true })}
        />
        {errors.name && <p>This field is required</p>}
      </div>
      <div>
        <label>Email</label>
        <input
          type="email"
          {...register('email', { required: true, pattern: /^\S+@\S+$/i })}
        />
        {errors.email && <p>Enter a valid email address</p>}
      </div>
      <button type="submit">Submit</button>
    </form>
  );
};

export default SimpleForm;

Enter fullscreen mode Exit fullscreen mode

In this section, we added two input fields for the user's name and email. We used the register function to connect the input fields to React Hook Form. We also added basic validation rules such as required and pattern for the email field.

5. Handling Form Submission

Now that we have our form set up with input fields and validation, let's handle the form submission.

// src/SimpleForm.js
import React from 'react';
import { useForm } from 'react-hook-form';

const SimpleForm = () => {
  const { register, handleSubmit, formState: { errors } } = useForm();

  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      {/* Form fields */}
      <button type="submit">Submit</button>
    </form>
  );
};

export default SimpleForm;

Enter fullscreen mode Exit fullscreen mode

In the onSubmit function, you can perform any actions you need, such as sending the form data to a server or updating the state of your application.

Conclusion

In this tutorial, we've covered the basics of setting up a form using React Hook Form. We installed the library, created a form component, added input fields with validation, and handled form submission. React Hook Form simplifies the process of managing forms in React applications and provides powerful tools for form validation and state management.

You can further customize your forms by adding more fields, complex validation rules, and integrating with other libraries. This tutorial serves as a foundation for building more advanced forms using React Hook Form.

Top comments (0)