DEV Community

Cover image for Stop Struggling with Forms in React - 4 Smart Ways to Handle Them
Werliton Silva
Werliton Silva

Posted on

5 2 3 2 1

Stop Struggling with Forms in React - 4 Smart Ways to Handle Them

Forms are an essential part of any web application. In React, there are several ways to manage forms, from manual control to powerful libraries. In this article, we’ll explore four popular methods to handle forms in React, with practical examples for each.


forms

1. Controlled Forms with React

React uses "controlled" inputs, where the input value is stored in the state.

import React, { useState } from 'react';

function BasicForm() {
  const [form, setForm] = useState({ name: '', email: '' });

  function handleChange(e) {
    setForm({ ...form, [e.target.name]: e.target.value });
  }

  function handleSubmit(e) {
    e.preventDefault();
    console.log(form);
  }

  return (
    <form onSubmit={handleSubmit}>
      <input placeholder="TCHACA" name="name" value={form.name} onChange={handleChange}  />
      <input placeholder="ARROCHA@TCHACA.COM" name="email" value={form.email} onChange={handleChange}  />
      <button type="submit">Submit</button>
    </form>
  );
}
Enter fullscreen mode Exit fullscreen mode

Highlights:

  • Uses event.target.name for multiple inputs.
  • Inputs are 100% controlled by state.

2. Formik

Formik

Formik is a popular library that allows you to handle forms in a declarative way.

import { Formik, Form, Field } from 'formik';

function FormikForm() {
  return (
    <Formik
      initialValues={{ name: '', email: '' }}
      onSubmit={(values) => console.log(values)}
    >
      <Form>
        <Field name="name" placeholder="Name" />
        <Field name="email" placeholder="Email" />
        <button type="submit">Submit</button>
      </Form>
    </Formik>
  );
}
Enter fullscreen mode Exit fullscreen mode

Highlights:

  • Less boilerplate.
  • Easy integration with validation tools.

3. Formik + Yup

Yup lets you define validation rules declaratively and works seamlessly with Formik.

import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';

const schema = Yup.object().shape({
  name: Yup.string().required('Name is required'),
  email: Yup.string().email('Invalid email').required('Email is required'),
});

function ValidatedForm() {
  return (
    <Formik
      initialValues={{ name: '', email: '' }}
      validationSchema={schema}
      onSubmit={(values) => console.log(values)}
    >
      <Form>
        <Field name="name" placeholder="Name" />
        <ErrorMessage name="name" component="div" />
        <Field name="email" placeholder="Email" />
        <ErrorMessage name="email" component="div" />
        <button type="submit">Submit</button>
      </Form>
    </Formik>
  );
}
Enter fullscreen mode Exit fullscreen mode

Highlights:

  • Powerful and reusable validation.
  • Instant feedback to users.
  • More examples here

See this live:


4. React Hook Form

RHF

A lightweight and performant alternative with built-in validation support.

import { useForm } from 'react-hook-form';

function RHFForm() {
  const { register, handleSubmit, formState: { errors } } = useForm();

  function onSubmit(data) {
    console.log(data);
  }

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

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

      <button type="submit">Submit</button>
    </form>
  );
}
Enter fullscreen mode Exit fullscreen mode

Highlights:

  • Better performance for large forms.
  • Less code and more focus on productivity.

Conclusion

These four approaches range from basic to advanced and cover the most common use cases for handling forms in React. Choose the one that best fits your project's needs.

Useful Resources:

Dynatrace image

Frictionless debugging for developers

Debugging in production doesn't have to be a nightmare.

Dynatrace reimagines the developer experience with runtime debugging, native OpenTelemetry support, and IDE integration allowing developers to stay in the flow and focus on building instead of fixing.

Learn more

Top comments (10)

Collapse
 
dotallio profile image
Dotallio

Great breakdown! React Hook Form made big forms so much easier for me, especially with those built-in validations - do you have a go-to for dynamic field arrays?

Collapse
 
werliton profile image
Werliton Silva

yeh. I like so much React Hook Form. About you question, do you have an example?

Collapse
 
michael_liang_0208 profile image
Michael Liang

Great post.

Collapse
 
werliton profile image
Werliton Silva

Thanks bro

Collapse
 
mannyai profile image
MannyAI

Good job!

Collapse
 
werliton profile image
Werliton Silva

Thank you!

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Insane how much easier this makes form stuff, I’ve lost way too many hours to messy state bugs

Collapse
 
werliton profile image
Werliton Silva

Yes. I'm too. many many hours. rssr

Collapse
 
dgdrh profile image
yooket34

From fully manual to library-powered, React offers many ways to build forms.

Collapse
 
werliton profile image
Werliton Silva

yes. for this I like so much React too

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

Debugging and Monitoring Basics for Builders

What is monitoring, and why does it matter for your hackathon project? In this kickoff session, we’ll cover the basics of debugging and introduce you to tools like Sentry that help you catch issues early. Learn how to make your app more stable, secure, and future-proof—so it’s not just ready for submission, but ready for whatever comes next.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️