How do you build forms in React with React Hook Form and Zod?
React Hook Form (RHF) keeps inputs uncontrolled (it reads values through refs), so typing does not re-render the whole form; it manages values, validation, errors, touched/dirty state and submission. Zod defines a schema once and gives both runtime validation and the TypeScript type.
Key APIs
useForm({ resolver: zodResolver(schema), defaultValues }).register('field')connects a native input;Controller/useControllerwraps controlled components (date pickers, selects from UI libraries).handleSubmit(onValid);formState.errors,isSubmitting,isDirty,isValid.useFieldArrayfor dynamic lists (order lines, phone numbers).watch/useWatchto react to values;setErrorto show server-side errors.- Validation mode:
onSubmit(default),onBlur,onChange,onTouched.
Accessibility
Link errors to inputs with aria-invalid and aria-describedby, keep a real <label>, and focus the first invalid field (RHF does this by default).
Example
tsx
import { useForm, useFieldArray } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
const schema = z.object({
email: z.string().email('Enter a valid email'),
age: z.coerce.number().int().min(18, 'You must be 18 or older'),
skills: z.array(z.object({ name: z.string().min(1, 'Required') })).min(1, 'Add at least one skill'),
});
type FormValues = z.infer<typeof schema>;
export function ProfileForm() {
const { register, control, handleSubmit, setError, formState: { errors, isSubmitting } } = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: { email: '', age: 18, skills: [{ name: '' }] },
mode: 'onTouched',
});
const { fields, append, remove } = useFieldArray({ control, name: 'skills' });
const onSubmit = async (values: FormValues) => {
const res = await fetch('/api/profile', { method: 'POST', body: JSON.stringify(values) });
if (res.status === 409) setError('email', { message: 'Email already registered' });
};
return (
<form onSubmit={handleSubmit(onSubmit)} noValidate>
<label htmlFor="email">Email</label>
<input id="email" {...register('email')} aria-invalid={!!errors.email} aria-describedby="email-err" />
{errors.email && <p id="email-err" role="alert">{errors.email.message}</p>}
<label htmlFor="age">Age</label>
<input id="age" type="number" {...register('age')} />
{errors.age && <p role="alert">{errors.age.message}</p>}
{fields.map((f, i) => (
<div key={f.id}>
<input {...register(`skills.${i}.name`)} placeholder="Skill" />
<button type="button" onClick={() => remove(i)}>Remove</button>
</div>
))}
<button type="button" onClick={() => append({ name: '' })}>Add skill</button>
<button disabled={isSubmitting}>{isSubmitting ? 'Saving…' : 'Save'}</button>
</form>
);
}Follow-up questions interviewers ask
Try answering each one before you open it.
Should validation live only on the client?
No. Client validation is for user experience; the server must validate again because requests can bypass the UI. Sharing the Zod schema between a Node/Next.js backend and the frontend keeps the rules identical; a Spring backend repeats them with Bean Validation.