Lesson 15 of 30
50%
Forms and Controlled Inputs
A controlled input has its value driven by React state.
The user types. onChange updates state. The input shows that state. React is the single source of truth.
One text field
javascript
Why value and onChange together
If you set value and skip onChange, the field freezes. React keeps showing the old string.
If you skip value, the input is uncontrolled. The DOM owns the text. That is harder to reset and validate.
Checkbox
javascript
Checkboxes use checked and event.target.checked, not value.
Select
javascript
Several fields
You can use one state object, or one useState per field. Both are fine.
javascript
Tip: Always preventDefault on submit. Then read state. That is a React form.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.