ReactRouting & formsLesson 38 / 69

Controlled forms

Fill-gap30 min

Controlled forms

In a controlled form, React state is the single source of truth for inputs:

const [email, setEmail] = useState("");

<input value={email} onChange={(e) => setEmail(e.target.value)} />

It feels like more work than letting the DOM hold the value — until validation arrives: with state you can reject, transform, or disable as the user types, because the value is already yours.

The pattern to remember: derive isValid from the state instead of storing a separate flag. Two sources of truth means they disagree eventually.

Exercise

Build a signup form with controlled inputs. Disable the submit button until email contains "@" and the password is 8+ characters — derived, not stored.