Controlled vs uncontrolled input?
Try code in the playgroundUnderstand the concept
A controlled input takes its displayed value from React state, with changes flowing through an event handler. An uncontrolled input keeps its current value in the DOM until the application reads it. Controlled inputs make coordinated validation and UI changes convenient, while uncontrolled inputs can keep simple forms less state-heavy.
Important ideas
-
Controlled: React state supplies the value, updated through an event handler.
-
Uncontrolled: the DOM keeps the value; read it through a ref or form submission.
-
Validate on the server too; browser checks can be bypassed.
-
Do not switch the same input unexpectedly from an undefined value to a controlled string value; initialize it consistently.
Example — controlled input:
import { useState } from 'react';
function NameInput() {
const [name, setName] = useState('');
return (
<input
aria-label="Name"
value={name}
onChange={event => setName(event.target.value)}
/>
);
}- The event updates state; the next render supplies the input's displayed value.
Can you explain it without the notes?
Controlled vs uncontrolled input?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
A controlled input takes its displayed value from React state, with changes flowing through an event handler. An uncontrolled input keeps its current value in the DOM until the application reads it. Controlled inputs make coordinated validation and UI changes convenient, while uncontrolled inputs can keep simple forms less state-heavy.
Turn this concept into a clear answer.
- Choose based on how much the UI must react to each change, and validate submitted values on the server regardless.
Adapted from 06_REACT_NEXTJS.md · “Controlled vs uncontrolled input?”. Examples are study fragments; framework behavior can depend on your version.