All Blogs

ReactReact Hook FormForm HandlingForm ValidationWeb DevelopmentJavaScript

React Hook Form vs. Vanilla React Forms

Zain ul Abbdin
Zain ul AbbdinSenior Software Engineer
React Hook Form vs. Vanilla React Forms

Introduction

Forms are an essential part of any web application. Whether it’s a login form, a registration page, or a multi-step wizard, form handling plays a critical role in user interaction. In React, there are two common approaches to handling forms:

Vanilla React Forms — using basic React state and event handlers.

React Hook Form (RHF) — a lightweight library that simplifies form management in React.

React Hook Form Overview

React Hook Form is a library that helps you manage form state, validation, and submission with minimal re-renders. It uses React hooks like useForm() to handle form logic efficiently.

Installation:

npm install react-hook-form

Example

import React from "react";
import { useForm } from "react-hook-form";
export default function ReactHookFormExample() {
const { register, handleSubmit, formState: { errors } } = useForm();

const onSubmit = (data) => {
console.log("Form Submitted:", data);
};

return (
<form onSubmit={handleSubmit(onSubmit)}>
<input
{...register("name", { required: "Name is required" })}
placeholder="Name"
/>
{errors.name && <p>{errors.name.message}</p>}

      <input
        type="email"
        {...register("email", { required: "Email is required" })}
        placeholder="Email"
      />
      {errors.email && <p>{errors.email.message}</p>}

      <button type="submit">Submit</button>
    </form>

);
}

Vanilla React Form

Vanilla React forms are managed using useState and custom event handlers. This approach gives full control but can become verbose and hard to manage as the form grows.

Key Characteristics:

Full control over logic Manual handling of state and validation More boilerplate code Example

import React, { useState } from "react";
export default function VanillaReactFormExample() {
const [formData, setFormData] = useState({ name: "", email: "" });
const [errors, setErrors] = useState({});

const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};

const validate = () => {
const newErrors = {};
if (!formData.name) newErrors.name = "Name is required";
if (!formData.email) newErrors.email = "Email is required";
return newErrors;
};

const handleSubmit = (e) => {
e.preventDefault();
const validationErrors = validate();
if (Object.keys(validationErrors).length > 0) {
setErrors(validationErrors);
} else {
console.log("Form Submitted:", formData);
setErrors({});
}
};

return (

<form onSubmit={handleSubmit}>
<input
        name="name"
        value={formData.name}
        onChange={handleChange}
        placeholder="Name"
      />
{errors.name && <p>{errors.name}</p>}

      <input
        name="email"
        value={formData.email}
        onChange={handleChange}
        placeholder="Email"
      />
      {errors.email && <p>{errors.email}</p>}

      <button type="submit">Submit</button>
    </form>

);
}

Comparison: React Hook Form vs. Vanilla React Form

Setup

React Hook Form: Offers a quick and simple setup using the useForm hook. Vanilla React Form: Requires manual setup of state management and event handlers for each input field.

Validation

React Hook Form: Supports built-in validation and integrates easily with schema validation libraries like Yup or Zod. Vanilla React Form: Validation must be implemented manually, making the logic more verbose and prone to repetition.

Performance

React Hook Form: Optimized with minimal re-renders, making forms faster and more responsive. Vanilla React Form: Causes more re-renders due to frequent state updates, especially on every keystroke.

Code Size

React Hook Form: Less boilerplate and cleaner code thanks to built-in hooks. Vanilla React Form: More verbose due to manual state handling, event binding, and validation logic.

Flexibility

React Hook Form: Very flexible and integrates well with UI libraries and validation tools. Vanilla React Form: Offers full control but requires more custom logic for advanced use cases.

Learning Curve

*React Hook Form: *Slightly steeper due to the need to learn its API and abstractions like controllers and resolvers.

Vanilla React Form: Easier for beginners who are already familiar with React state and events.

Which One is Better?

Use React Hook Form if: You need better performance and less boilerplate You’re building complex or large forms You want built-in validation and integrations

Use Vanilla React Form if: You’re working on a very small form You prefer to have full control over the form logic You want to avoid extra dependencies

Conclusion

React Hook Form offers a cleaner and more scalable way to manage forms, especially for complex applications. It reduces boilerplate, improves performance, and simplifies validation. However, for very simple forms or when you need complete control, vanilla React forms are still a solid option.

Choosing the right approach depends on the complexity of your form and your project requirements. For most modern React apps, React Hook Form is a highly recommended solution.

About the Author

Zain ul Abbdin

Zain ul Abbdin

Senior Software Engineer

Connect on LinkedIn