Why onChange is Required with value
You'll learn: Why React forces you to provide an onChange handler when you set a value prop on an input.
The Read-Only Problem
When you give an input a value prop, React takes full control of what that input displays. The DOM can no longer change it on its own—React's state is now the single source of truth.
Here's what happens if you forget onChange:
const [name, setName] = useState("Alice");
return <input value={name} />;
// Input shows "Alice" but you can't type!
The input becomes read-only. Why? Because:
- The
valueprop tells the input to display"Alice" - When you try to type, the browser wants to update the DOM
- But React says "No—this input's value is controlled by state"
- Since there's no
onChangeto update state, the value never changes - React re-renders with the same state, overwriting any attempted DOM changes
The onChange Solution
To make the input editable, you must handle changes and update state:
const [name, setName] = useState("Alice");
return (
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
);
Now typing works because:
- User types →
onChangefires → state updates → React re-renders with new value
Without onChange, React prevents the DOM mutation from sticking, making the input effectively frozen.
Key Takeaway: The value prop makes an input controlled, but onChange is required to let users actually change that value—without it, you've created a read-only input.