Forms are one of the most important parts of modern web applications. Almost every application requires users to enter and submit information.
Common examples include:
Handling forms manually becomes difficult as applications grow, so React developers commonly use dedicated form libraries.
In this module, you'll learn about:
Throughout this course, we will primarily focus on React Hook Form.
Form handling is the process of collecting, validating, and submitting user input.
A complete form usually involves:
Example:
<form>
<input
type="email"
/>
<input
type="password"
/>
<button>
Login
</button>
</form>
Although this looks simple, real-world forms often contain dozens of fields and complex validation.
As forms become larger, they become harder to manage.
A registration form may include:
In addition, developers need to handle:
Managing all of this manually quickly becomes complicated.
This is why form libraries exist.
Some of the most popular React form libraries are:
For validation, developers commonly use:
Today, the most common combination is:
React Hook Form
+
Zod
Formik is one of the oldest and most popular React form libraries.
It simplifies:
npm install formik
import {
Formik,
Form,
Field
}
from "formik";
function LoginForm() {
return (
<Formik
initialValues={{
email: ""
}}
onSubmit={
values => {
console.log(
values
);
}
}
>
<Form>
<Field
name="email"
/>
<button>
Submit
</button>
</Form>
</Formik>
);
}
Formik offers:
Compared to modern alternatives, Formik has some drawbacks:
Because of these reasons, many modern React projects prefer React Hook Form.
React Hook Form (RHF) is a modern form library built specifically for React Hooks.
It focuses on performance and simplicity.
React Hook Form provides:
A traditional React form often looks like this:
const [email, setEmail] =
useState("");
const [
password,
setPassword
] = useState("");
Each input requires its own state.
As forms become larger, managing dozens of state variables becomes difficult.
React Hook Form solves this problem.
npm install react-hook-form
Import the hook:
import {
useForm
}
from "react-hook-form";
Create the form:
const {
register,
handleSubmit
} = useForm();
function LoginForm() {
const {
register,
handleSubmit
} = useForm();
const onSubmit =
data => {
console.log(data);
};
return (
<form
onSubmit={
handleSubmit(
onSubmit
)
}
>
<input
{...register(
"email"
)}
/>
<button>
Submit
</button>
</form>
);
}
register() WorksThe register() function connects an input element to React Hook Form.
Example:
<input
{...register(
"email"
)}
/>
When the form is submitted, React Hook Form automatically collects the value.
User enters:
samir@gmail.com
Submitted data:
{
email:
"samir@gmail.com"
}
<input
{...register(
"email"
)}
/>
<input
{...register(
"password"
)}
/>
Submitted result:
{
email:
"samir@gmail.com",
password:
"123456"
}
Validation ensures users enter correct and meaningful data.
Validation helps prevent:
<input
{...register(
"email",
{
required:
"Email is required"
}
)}
/>
If the user leaves the field empty, React Hook Form displays the specified error message.
const {
register,
handleSubmit,
formState: {
errors
}
} = useForm();
Display the error:
{
errors.email &&
<p>
{
errors.email.message
}
</p>
}
<input
{...register(
"email",
{
required:
"Email required",
pattern: {
value:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/,
message:
"Invalid email"
}
}
)}
/>
This validates that the input follows a proper email format.
<input
{...register(
"password",
{
minLength: {
value: 6,
message:
"Minimum 6 characters"
}
}
)}
/>
<input
{...register(
"username",
{
required:
"Username required",
minLength: {
value: 3,
message:
"Minimum 3 characters"
}
}
)}
/>
| Rule | Purpose |
|---|---|
required |
Required field |
minLength |
Minimum length |
maxLength |
Maximum length |
min |
Minimum numeric value |
max |
Maximum numeric value |
pattern |
Regular expression validation |
validate |
Custom validation |
<input
{...register(
"age",
{
validate:
value =>
value >= 18 ||
"Must be 18+"
}
)}
/>
Custom validation allows developers to define their own validation logic.
User Input
│
▼
register()
│
▼
Validation
│
▼
Errors
│
▼
Submit
A very common modern stack is:
React
│
▼
React Hook Form
│
▼
Shadcn UI
│
▼
Validation
Example:
<FormField
control={form.control}
name="email"
render={({ field }) => (
<Input
{...field}
/>
)}
/>
This integrates Shadcn UI components with React Hook Form.
React Hook Form handles form state, but validation is often performed using dedicated validation libraries.
Popular choices include:
Among these, Zod is the most commonly used.
Zod is a schema validation library.
It allows developers to define validation rules in one reusable schema.
Install Zod:
npm install zod
import { z }
from "zod";
const schema =
z.object({
email:
z.string().email(),
password:
z.string().min(6)
});
This schema validates:
Zod provides:
It integrates very well with React Hook Form.
| Feature | Formik | React Hook Form |
|---|---|---|
| Performance | Good | Excellent |
| Re-renders | More | Fewer |
| Boilerplate | More | Less |
| Popularity | High | Very High |
| Modern Usage | Moderate | Very High |
React Hook Form is now the preferred choice for most modern React applications.
The recommended stack throughout this course is:
React
│
▼
React Hook Form
│
▼
Zod Validation
│
▼
Shadcn UI
This combination provides:
Prefer React Hook Form over manually managing dozens of useState() variables.
Always validate:
Keep validation logic separate from UI by defining reusable Zod schemas.
Instead of:
Invalid Input
Use:
Email is required
Password must contain at least 6 characters
This improves user experience.
For larger forms, separate:
This makes forms easier to maintain.
| Tool | Purpose |
|---|---|
| Formik | Traditional React form library |
| React Hook Form | Modern React form handling |
register() |
Register input fields |
handleSubmit() |
Handle form submission |
| Validation | Verify input correctness |
| Errors | Display validation messages |
| Zod | Schema validation library |
Several form libraries exist for React, including Formik, but modern React applications primarily use:
React Hook Form
│
▼
Validation
│
▼
Shadcn UI
This combination provides excellent performance, simple APIs, and modern development patterns.
A typical form workflow looks like this:
User Input
│
▼
React Hook Form
│
▼
Validation
│
▼
Submit Data
│
▼
API Request
Mastering React Hook Form and validation libraries such as Zod enables you to build robust, user-friendly forms for authentication, registration, checkout, settings, and many other real-world applications.