React Hook Form fundamentals and validation
This handbook targets React Hook Form 7.82.0.
React Hook Form 8 is still beta at this audit point, so this section teaches the current stable v7 API.
React Hook Form manages form state, not general application state.
Mental modelโ
Why a form library existsโ
A simple form may need only native HTML + FormData.
A complex form often needs:
- field registration;
- validation;
- nested values;
- dirty/touched tracking;
- error state;
- conditional fields;
- dynamic arrays;
- controlled component integration;
- reset/default-value workflows;
- isolated subscriptions.
React Hook Form packages those concerns into a dedicated form model.
Installโ
npm install react-hook-form
Basic useFormโ
import { useForm } from 'react-hook-form'
type LoginForm = {
email: string
password: string
}
function Login() {
const {
register,
handleSubmit,
formState: { errors, isSubmitting },
} = useForm<LoginForm>()
async function onSubmit(values: LoginForm) {
console.log(values)
}
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
{...register('email', {
required: 'Email is required',
})}
/>
{errors.email && (
<p id="email-error" role="alert">
{errors.email.message}
</p>
)}
<label htmlFor="password">Password</label>
<input
id="password"
type="password"
{...register('password', {
required: 'Password is required',
minLength: {
value: 8,
message: 'Use at least 8 characters',
},
})}
/>
<button disabled={isSubmitting} type="submit">
Sign in
</button>
</form>
)
}
What register providesโ
Conceptually:
The current API connects fields through returned props such as name, ref, onChange, and onBlur.
Validation rulesโ
Built-in field rules include common constraints such as:
required;min/max;minLength/maxLength;pattern;- custom
validatefunctions.
<input
{...register('username', {
required: 'Username is required',
validate: (value) =>
value.trim().length > 2 || 'Use at least 3 characters',
})}
/>
Client validation improves UX. It does not replace server validation.
handleSubmitโ
onSubmit(values).handleSubmit orchestrates validation before your submit callback.
formStateโ
Important form metadata includes concepts such as:
Do not destructure everything automatically. Subscribe to the state the component actually needs.
Default valuesโ
Use defaultValues as the form's initial source of truth.
const form = useForm<ProfileForm>({
defaultValues: {
firstName: '',
lastName: '',
},
})
Default values matter for reset and dirty-state comparison.
Async default valuesโ
React Hook Form supports async default-value workflows in current v7 APIs.
The important architecture is:
Do not continuously overwrite user edits whenever server data refetches.
Dirty vs touchedโ
These are different concepts.
Watch valuesโ
For small cases:
const plan = watch('plan')
For targeted subscription behavior, use APIs such as useWatch rather than forcing the entire form owner to respond to every field change.
Subscription granularity is one of React Hook Form's important architectural advantages.
Validation schemasโ
React Hook Form integrates with ecosystem schema libraries through resolver packages.
Examples include Zod, Yup, and others.
Keep the trust boundary clear:
A TypeScript type is not runtime validation.
Accessibilityโ
Keep semantic HTML.
<label htmlFor="email">Email</label>
<input
id="email"
aria-invalid={Boolean(errors.email)}
aria-describedby={errors.email ? 'email-error' : undefined}
{...register('email', { required: 'Email is required' })}
/>
{errors.email && (
<p id="email-error" role="alert">
{errors.email.message}
</p>
)}
React Hook Form does not automatically make custom form UIs accessible.
Server error mappingโ
A server may reject values that passed client validation.
Use APIs such as setError when a server/domain error belongs to a specific field or the form root.
Resetโ
After a successful save, reset can establish new default values.
reset(savedProfile)
This matters because dirty state is compared against defaults.
Form state vs server mutation stateโ
Values ยท field errors ยท dirty/touched ยท local form validation.
Request ยท server error ยท cache invalidation ยท remote reconciliation.
Do not force one tool to own both lifecycles.
Common mistakesโ
- putting a basic two-field form into a form library without need;
- trusting browser validation on the server;
- forgetting stable/default values for dirty tracking;
- watching the entire form when only one field matters;
- duplicating RHF values into Redux/Zustand on every keystroke;
- treating TypeScript types as validation.
Exerciseโ
Build a profile form with:
- typed values;
- default values;
- required email validation;
- accessible errors;
- dirty-state save button;
- server-side email uniqueness error mapped back to the field;
- reset after successful save.
Interview questionsโ
Junior: What do useForm, register, and handleSubmit do?
Mid-level: What is the difference between dirty and touched state?
Senior: Why should server validation still run when React Hook Form validation passes?
Staff: How would you prevent a complex form from duplicating ownership across React Hook Form, Redux, and a server-state cache?