ReactQuestion 10 of 28
Controlled and uncontrolled components?
Answer out loud or in your head first, then compare.
Answer
Controlled input:
JSX
const [name, setName] =
useState("");
<input
value={name}
onChange={e =>
setName(e.target.value)
}
/>React controls the value.
Uncontrolled:
JSX
const inputRef = useRef();
<input ref={inputRef} />The DOM holds the value.
Controlled inputs make these easier:
- validation,
- synchronisation,
- controlling the form.
How did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- C-P07 · What is a controlled form field? — React, Next.js and frontend
Practise in code
- Search on button click — guided self-review