Course contentsShow
← React
Lesson 466 of 2,39613. Controlled ComponentsFree lesson

Why onChange is Required with value

Without onChange, inputs become read-only because React prevents DOM mutations conflicting with state.

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:

  1. The value prop tells the input to display "Alice"
  2. When you try to type, the browser wants to update the DOM
  3. But React says "No—this input's value is controlled by state"
  4. Since there's no onChange to update state, the value never changes
  5. 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 → onChange fires → 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.