Courses / React 19: From Components to Production / Chapter 18
Module 9 · Complex Forms and Testing · Chapter 18 of 30
Complex Forms and Multi-Step Workflows
Builds on Components: Props, Children, Conditions, Lists and Keys, Forms: Controlled and Uncontrolled Inputs, Validation and Form Actions, Advanced Hooks: Refs, Reducers, Context, Layout Effects and Custom Hooks, Data Fetching: Requests, Loading States, Races, Caching and Errors and Real-Time, Background Work and Offline Support. Next up: Testing React: Components, Hooks, Network and End-to-End.
Part 1 · When a form needs a library: register and the cost of renders
The cost of a controlled form
A home loan application has dozens of fields across applicant, income, property and bank sections. The forms chapter showed that a controlled input renders the component that owns its state once per keystroke, which is harmless for one field and starts to matter for forty.
The test below builds a form of 40 text fields in one component, all controlled from one state object. Each field is a small component that counts its renders. The test types four characters into one field.
function ControlledForm() { counts.form += 1 const [values, setValues] = useState<Record<string, string>>(() => Object.fromEntries(names.map((n) => [n, ''])), ) return ( <form> {names.map((n) => ( <Field key={n} name={n} value={values[n]} onChange={(v) => setValues({ ...values, [n]: v })} /> ))} </form> ) }
controlled, after mount: form 1, field 40 controlled, after typing 4 characters in one field: form 4, field 160
Output of a real run in jsdom, StrictMode off. First two lines.