Build forms in React, without the tears
Opening the entry
Opening the entry
React Hooks for form state management and validation (Web + React Native)
React Hook Form is a library for managing form state and validation in React and React Native applications. It uses native HTML form validation and provides a small, dependency-free core. The library integrates with popular schema validators like Zod, Yup, and Joi through resolver adapters.
npm install 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>
);
}react-hook-form/react-hook-form is a third-party open-source project, listed here from public GitHub data. ImuTechz did not write it and has not reviewed or security-audited its code. Read the source and its permissions before you install or run it.