Sub topic 15
Choosing between hand-written forms and a form library
This chapter assumes you can build a controlled form with useState, validate values in a pure function, and read an uncontrolled form with FormData. It helps you decide when that hand-written approach is enough and when a library is worth adding.
What hand-written forms cost
A controlled form needs the same pieces every time:
- A state object for the values.
- A change handler.
- A
validatefunction. - An errors object.
- A touched record, if you show errors after a user leaves a field.
- Reset logic.
For a login form this is about 40 lines and easy to follow. As a form grows, you repeat that wiring for every field, and the bugs tend to sit in the wiring: a forgotten name attribute, stale touched flags, or errors that never clear.
Controlled inputs also re-render the whole component on every keystroke. With ten fields this is invisible. With a few hundred fields, or an expensive sibling component, typing can lag.
What a library gives you
Form libraries such as React Hook Form and Formik take over the repeated wiring. They typically provide:
- Registration of fields by name.
- Tracking of values, errors, touched, dirty, and submitting state.
- Submit handling that runs validation first and calls your callback only with valid values.
- Integration with schema validators such as Zod or Yup.
- Helpers for dynamic field arrays.
React Hook Form registers inputs as uncontrolled and reads them through refs. This means typing does not re-render the form. Formik keeps values in React state, so it behaves like the controlled pattern with the bookkeeping done for you.
Here is the login form from the validation chapter, written with React Hook Form. It assumes you have run npm install react-hook-form.
import { useForm } from 'react-hook-form';
export default function LoginForm() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({ defaultValues: { email: '', password: '' } });
function onSubmit(values) {
console.log('Valid', values);
}
return (
<form onSubmit={handleSubmit(onSubmit)} noValidate>
<label htmlFor="email">Email</label>
<input
id="email"
aria-invalid={Boolean(errors.email)}
aria-describedby={errors.email ? 'email-error' : undefined}
{...register('email', {
required: 'Enter your email address.',
pattern: { value: /\S+@\S+/, message: 'Enter a valid email address.' },
})}
/>
{errors.email && <p id="email-error">{errors.email.message}</p>}
<label htmlFor="password">Password</label>
<input
id="password"
type="password"
aria-invalid={Boolean(errors.password)}
aria-describedby={errors.password ? 'password-error' : undefined}
{...register('password', {
minLength: { value: 8, message: 'Use at least 8 characters.' },
})}
/>
{errors.password && <p id="password-error">{errors.password.message}</p>}
<button type="submit">Log in</button>
</form>
);
}The accessibility attributes are still your job. A library manages state, not markup.
Deciding
| Situation | Suggested approach |
|---|---|
| Search box, newsletter signup | Uncontrolled with `FormData` |
| Login or short form with live feedback | Controlled with `useState` |
| Long form, repeated across the app | Form library |
| Field arrays, multi-step wizards, shared schema with the server | Form library with a schema validator |
| Hundreds of fields | Uncontrolled-first library |
Common mistakes
- Adding a library to a two-field form. You add a dependency and an API to learn, and save almost nothing.
- Avoiding a library for a form that has grown to 30 fields and its own bug tracker.
- Mixing styles on one field, such as
registertogether with avalueprop. - Assuming a library handles accessibility. You still write the labels and error links.
Exercise
A settings page has 40 fields, some added dynamically, and typing feels slow. The team validates against a Zod schema that the server also uses. Which approach fits best, and why?
Show answer
A form library that is uncontrolled-first, such as React Hook Form with a Zod resolver. Uncontrolled registration avoids re-rendering all 40 fields on each keystroke. It handles dynamic field arrays, and the shared schema keeps client and server validation consistent.