Welcome to our comprehensive guide on React Hook Form! In this tutorial, we'll delve into the world of form handling in React using the popular library, React Hook Form. By the end of this lesson, you'll be able to create efficient and user-friendly forms in your React projects.
React Hook Form is a powerful, opinionated form-building library for React. It simplifies form handling and validations, making it easier to create complex forms without writing a lot of code.
Before diving into React Hook Form, make sure you have React and Node.js installed on your machine. You can create a new React app using create-react-app:
npx create-react-app my-app
cd my-appTo install React Hook Form, run:
npm install react-hook-formLet's create a simple form to demonstrate the basics of React Hook Form.
import React from 'react';
import { useForm } from 'react-hook-form';
function Example() {
const {
register,
handleSubmit,
errors,
} = useForm();
const onSubmit = data => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>
Name:
<input type="text" ref={register({ required: true })} />
{errors.name && <span>This field is required</span>}
</label>
<input type="submit" />
</form>
);
}
export default Example;In this example, we import useForm from React Hook Form and use its returned objects (register, handleSubmit, and errors) to create a simple form. The form includes a name field with validation for required inputs.
React Hook Form provides various validation rules out-of-the-box. You can also create custom validation rules to suit your needs.
Here's an example of using custom validation rules:
import { useForm, ValidationError } from 'react-hook-form';
function Example() {
const {
register,
handleSubmit,
errors,
} = useForm();
const onSubmit = data => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>
Email:
<input type="text" ref={register({ required: true, pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ })} />
{errors.email && <ValidationError message="Invalid email address" />}
</label>
<input type="submit" />
</form>
);
}
export default Example;In this example, we've added a custom validation rule for email addresses. If the provided email is invalid, a ValidationError component will be rendered, displaying an error message.
React Hook Form provides various form controls, including TextInput, Select, and Checkbox. Here's an example of using a Select control:
import { useForm } from 'react-hook-form';
function Example() {
const {
register,
handleSubmit,
errors,
} = useForm();
const onSubmit = data => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>
Favorite Color:
<select ref={register} defaultValue="red">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</label>
<input type="submit" />
</form>
);
}
export default Example;In this example, we've used the Select control with the register function to register the control for form handling.
What does `useForm` return from React Hook Form?
We hope this tutorial has helped you understand the basics of React Hook Form. With these concepts, you're well on your way to creating efficient and user-friendly forms in your React projects. Happy coding! 🚀💻