Hint
Controlled = React owns the value; uncontrolled = DOM owns it (accessed via ref)
Controlled component — React state is the single source of truth. Every change goes through setState.
Uncontrolled component — the DOM stores the value. You pull it with a ref when needed.
// Controlled — React owns the value
function Controlled() {
const [value, setValue] = useState('');
return (
setValue(e.target.value)}
/>
);
}
// Uncontrolled — DOM owns the value
function Uncontrolled() {
const inputRef = useRef(null);
function handleSubmit() {
console.log(inputRef.current.value); // pull on demand
}
return ;
}
When to use each:
<input type="file">) are always uncontrolled — you can't set their value programmatically for security reasons.