react-hook-form
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
react | 18.2.0 | 79.25 kB | MIT | prod peer |
React-hook-form is a highly performant, flexible, and extensible forms library designed specifically for React Hooks. It focuses on providing excellent user experience and developer experience. React-hook-form embraces the native HTML form validation, enabling integration with several UI libraries out of the box. Moreover, it's a lightweight package with no dependencies and supports popular validation libraries such as Yup, Zod, AJV, Superstruct, Joi, among others.
To use react-hook-form in your React application, first, you need to install the npm package using the command npm install react-hook-form
. Here's a simple example demonstrating the usage of react-hook-form.
import { useForm } from 'react-hook-form';
function App() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm();
return (
<form onSubmit={handleSubmit((data) => console.log(data))}>
<input {...register('firstName')} />
<input {...register('lastName', { required: true })} />
{errors.lastName && <p>Last name is required.</p>}
<input {...register('age', { pattern: /\d+/ })} />
{errors.age && <p>Please enter a number for age.</p>}
<input type="submit" />
</form>
);
}
In this code, the useForm
hook from react-hook-form is called, which provides functions and data to manage form states. The register
function is used for collecting input data, the handleSubmit
function is used for handling form submission and formState
object is used to get any error messages.
For a detailed understanding and learning of react-hook-form, you can refer to their documentation at React Hook Form API. The documentation offers a comprehensive guide on how to get started, API details, FAQs, and a wide range of practical examples to use the library effectively for implementing form validations in your React applications.