Controlled vs Uncontrolled Components in React
In a controlled component, React state holds the value of a form field and you change it through an onChange handler. In an uncontrolled component, the browser's DOM holds the value and you read it only when you need it, usually with a ref or FormData. The difference is simply: who owns the value, React or the DOM?
This is a very common React interview question, and it also explains one of the most common console warnings you'll see. Let's go through it step by step.
Start with how plain HTML works
Before React, an <input> already knew how to store what you type. You type, the browser remembers it, and when the form is submitted you read it out. The input is its own little piece of state.
React gives you a choice. You can leave that behavior alone (uncontrolled), or you can take over and say "React state is the only source of truth for this field" (controlled).
Controlled components
A controlled input gets its value from state, and updates that state in onChange:
function NameForm() {
const [name, setName] = useState('');
return (
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
);
}
Here's what happens on every keystroke:
- You press a key.
- React calls
onChangewith the event. - You call
setName(e.target.value), which updates state. - React re-renders, and the input receives the new
value.
So the text you see on screen is always exactly what's in state. If you removed the setName call, the input would not change at all, because React keeps putting the old value back. That's what "controlled" means.
Because the value lives in state, you can do things with it on every change:
// Only allow digits, and show the value live
onChange={(e) => setPhone(e.target.value.replace(/\D/g, ''))}
Uncontrolled components
An uncontrolled input keeps its own value in the DOM. React doesn't track it while you type. You set a starting value with defaultValue and read the current value later.
function NameForm() {
const inputRef = useRef(null);
function handleSubmit(e) {
e.preventDefault();
console.log(inputRef.current.value);
}
return (
<form onSubmit={handleSubmit}>
<input ref={inputRef} defaultValue="Ann" />
<button>Save</button>
</form>
);
}
Two things to notice:
defaultValueis only the initial value. The user can edit the field freely, and changingdefaultValuelater does nothing. (For checkboxes the matching prop isdefaultChecked.)- A ref is how you reach the real DOM element to read its value.
You can also skip refs by giving each field a name and reading them all at once with the browser's FormData:
function handleSubmit(e) {
e.preventDefault();
const data = new FormData(e.currentTarget);
console.log(data.get('email'));
}
// <input name="email" type="email" />
value vs defaultValue
These two props are the heart of the question:
| Controlled | Uncontrolled | |
|---|---|---|
| Who holds the value | React state | The DOM |
| Initial value prop | value | defaultValue |
Needs onChange | Yes | No |
| Read the value | From state, any time | From a ref or FormData |
| Re-renders while typing | Yes, on every change | No |
| Live validation / formatting | Easy | Harder |
If you pass value but no onChange, React shows a warning in the console and the field becomes read-only. The fix depends on what you meant: use defaultValue for an initial value, add onChange to control it, or add readOnly if it's meant to be read-only.
The "uncontrolled to controlled" warning
You may have seen this message:
A component is changing an uncontrolled input to be controlled.
React doesn't let an input switch modes during its lifetime. The usual cause is state that starts as undefined:
const [name, setName] = useState(); // undefined at first
<input value={name} onChange={(e) => setName(e.target.value)} />
On the first render value is undefined, so React treats the input as uncontrolled. As soon as you type, value becomes a string, and the input has switched sides. The fix is to make sure a controlled input always gets a string:
const [name, setName] = useState('');
The same thing happens when you load data from a server and the field is undefined until the response arrives. Give it a fallback, like value={user.name ?? ''}.
Which one should you use?
A simple rule: if you need to react to every change, use controlled. If you only need the value at the end, uncontrolled is simpler.
- Controlled is the right choice for live validation, input masks, enabling or disabling a button as the user types, and fields that depend on each other.
- Uncontrolled works well for simple forms, and it's the only option for file inputs:
<input type="file">is always uncontrolled, because you can't set its value from code. - Large forms can feel slow when every keystroke re-renders the whole form. Uncontrolled fields (or a form library built on them) avoid that. Rendering cost is covered in why a component re-renders.
Resetting a form
How you reset depends on the type:
- Uncontrolled: call
form.reset(), which puts fields back to their default values. - Controlled: set the state back to its initial values.
- Either: change the
keyof the component that renders the form. React throws away the old component and its DOM and creates a fresh one, so both the fields and local state reset. This is the same idea as in the React key prop.
Examples
1. An input you can't type in
<input value="hello" />
Problem: the field is stuck on "hello" and the console shows a warning.
Why: value makes it controlled, but nothing ever updates the value, so React keeps showing "hello".
Fix: use defaultValue if "hello" is just a starting point, or add state and onChange.
<input defaultValue="hello" />
2. State that starts as undefined
const [email, setEmail] = useState();
Problem: the "uncontrolled to controlled" warning appears the first time you type.
Why: the value goes from undefined to a string, so the input changes modes.
Fix: start with an empty string.
const [email, setEmail] = useState('');
3. Controlling a file input
<input type="file" value={file} onChange={...} />
Problem: this doesn't work, and you can't pre-fill a file.
Why: a file input's value is read-only for security, so it is always uncontrolled.
Fix: read the files from the event or a ref, and store them in state if you need them elsewhere.
<input
type="file"
onChange={(e) => setFile(e.target.files[0])}
/>
4. A form that only needs values on submit
function Login() {
function handleSubmit(e) {
e.preventDefault();
const data = new FormData(e.currentTarget);
login(data.get('email'), data.get('password'));
}
return (
<form onSubmit={handleSubmit}>
<input name="email" type="email" />
<input name="password" type="password" />
<button>Log in</button>
</form>
);
}
How it works: no state, no re-renders while typing. If you later need live validation on one field, make just that field controlled. You can mix both in one form.
Interview answer
A controlled component has its value stored in React state. The input shows
valuefrom state and updates it inonChange, so React is the single source of truth. An uncontrolled component lets the DOM store the value, you set the start withdefaultValueand read it with a ref or FormData when you need it. I use controlled inputs when I need live validation, formatting or dependent fields, and uncontrolled inputs for simple forms and file inputs. An input can't switch between the two, which is why state should start as an empty string instead of undefined.
Summary
- Controlled: React state owns the value (
value+onChange). - Uncontrolled: the DOM owns the value (
defaultValue+ a ref orFormData). - A controlled input re-renders on every change. An uncontrolled one doesn't.
valuewithoutonChangemakes a read-only field and logs a warning.defaultValue(anddefaultChecked) set only the initial value.- Don't switch modes: start state as
'', notundefined, to avoid the "uncontrolled to controlled" warning. - File inputs are always uncontrolled.
- Choose controlled for live validation and dependent fields, uncontrolled for simple forms.
- You can mix both in one form.
- Changing a component's
keyresets the form and its fields.