Best Practices for Form Inputs in React

Building form inputs in React that are easy to use and maintain.

Forms in React tend to start simple and slowly turn into a mess of state variables, change handlers and validation logic. Most of that can be avoided with a few habits that keep your inputs predictable and easy to use.

tl;dr Use controlled inputs, one change handler, a real label for every field, the right input type, and let a form element handle submitting.

Use Controlled Inputs

A controlled input gets its value from React state and updates that state on every change. With hooks this takes a single useState call:

import React, { useState } from "react"
function NewsletterForm() {
const [email, setEmail] = useState("")
return (
<input
type="email"
value={email}
onChange={(event) => setEmail(event.target.value)}
/>
)
}

React state is now the single source of truth, so you always know what's in the input and can reset it with setEmail("").

One Change Handler for Every Field

Writing a handler for every field gets old fast. Instead, keep the form values in one object and use each input's name to update the right key:

const [values, setValues] = useState({ name: "", email: "" })
const handleChange = (event) => {
const { name, value } = event.target
setValues({ ...values, [name]: value })
}

This uses destructuring to pull out name and value, and the spread operator to copy the rest of the values. Every input just needs a name that matches a key:

<input name="email" value={values.email} onChange={handleChange} />

Always Use a Label

A placeholder is not a label. It disappears as soon as someone starts typing, and screen readers don't treat it the same way. Give every input a <label> and connect them with htmlFor and id:

<label htmlFor="email">Email</label>
<input id="email" name="email" value={values.email} onChange={handleChange} />

Clicking the label now focuses the input, which is a nice bonus on mobile.

Pick the Right Type and autoComplete

The type attribute decides which keyboard shows up on phones. email, tel and number all give the user a better keyboard than a plain text input. The autoComplete attribute lets the browser fill in what it already knows:

<input
id="email"
type="email"
name="email"
autoComplete="email"
value={values.email}
onChange={handleChange}
/>

Other useful values are name, tel, street-address, new-password and current-password.

Submit with a Form Element

Wrap your inputs in a <form> and handle onSubmit instead of putting an onClick on the button. This way pressing Enter in any field submits the form, just like users expect:

const handleSubmit = (event) => {
event.preventDefault() // stops the page from reloading
console.log(values)
}
return (
<form onSubmit={handleSubmit}>
...
<button type="submit">Sign up</button>
</form>
)

Validate Without Getting in the Way

The browser already has validation built in. Adding required, minLength or type="email" stops the form from submitting until the field is valid:

<input id="email" type="email" name="email" required ... />

For custom messages, keep an errors object in state and show the error right below the field it belongs to. Check fields when the form is submitted or when the user leaves a field (onBlur), not on every keystroke. Nobody wants to be told their email is invalid after typing the first letter.

const [errors, setErrors] = useState({})
const handleBlur = (event) => {
const { name, value } = event.target
setErrors({ ...errors, [name]: value ? "" : "This field is required" })
}
...
<input id="email" name="email" onBlur={handleBlur} ... />
{errors.email && <p className="error">{errors.email}</p>}

For bigger forms, a library like Formik handles a lot of this for you, but the same habits still apply.

Have some feedback on this post? .