Sub topic 2
Registering inputs with register
This chapter assumes you know React function components and hooks, TypeScript generics, and how a browser form element fires its events. You should have seen useForm once. Here you will connect inputs to it.
What register does
register is a function returned by useForm. You call it with a field name, and it returns four props: name, onChange, onBlur, and ref. Spreading those props onto an input tells React Hook Form to track that element.
The input stays uncontrolled. React does not store its value in state. The library holds a reference to the DOM element through ref and reads the value when it needs it, for example on submit. Typing in a registered field does not re-render your component.
A first registered form
The field name is typed against the generic you pass to useForm. A misspelled name is a compile error.
import { useForm } from "react-hook-form";
type ProfileValues = {
name: string;
age: number;
newsletter: boolean;
};
export function ProfileForm() {
const { register, handleSubmit } = useForm<ProfileValues>({
defaultValues: { name: "", age: 18, newsletter: false },
});
return (
<form onSubmit={handleSubmit((values) => console.log(values))}>
<label htmlFor="name">Name</label>
<input id="name" {...register("name")} />
<label htmlFor="age">Age</label>
<input
id="age"
type="number"
{...register("age", { valueAsNumber: true })}
/>
<label>
<input type="checkbox" {...register("newsletter")} />
Subscribe
</label>
<button type="submit">Save</button>
</form>
);
}Three details matter here:
- DOM inputs produce strings.
valueAsNumber: trueconverts the value to a number before it reaches your submit handler. - A checkbox registered with one name gives a
boolean. - Initial values come from
defaultValuesonuseForm, not from avalueordefaultValueprop on the input.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Field is missing from submitted data | `register` was called but its result was not spread | Write `{...register("name")}` |
| Value is `undefined` or never updates | The custom component does not pass `ref` to a DOM element | Forward the ref to the inner `input` |
| Input will not accept typing | You added `value={...}` without a matching `onChange` | Remove `value`, or use `Controller` for controlled components |
| Number field submits `"42"` | The DOM returned a string | Use `valueAsNumber`, or convert in the schema |
| Empty number field gives `NaN` | `valueAsNumber` turns an empty string into `NaN` | Validate it in the schema, covered in later chapters |
Nested data uses dot paths, such as register("address.city"). Array rows use indexed paths, such as register("items.0.name"). The paths are type-checked against your form type.
Exercise
A teammate writes <input value={name} {...register("name")} /> and finds that typing does nothing. Why does this happen, and what are two ways to fix it?
Show answer
Setting value without an onChange that updates name makes React treat the input as a controlled field with a fixed value, so typing has no effect. Fix it by removing the value prop and setting the initial value in defaultValues, or by switching the field to Controller so the form manages a controlled component properly.