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

The value Prop on Inputs

Setting value on an <input> makes it read-only unless paired with onChange—React takes full control of the input's display.

The value Prop on Inputs

What you'll learn: How setting value on an <input> makes React take full control of what the user sees.


The Power and Peril of value

When you pass a value prop to an <input>, React overrides the browser's default behavior. Normally, an input field manages its own displayed text—the browser remembers what you type. But once you add value={something}, React becomes the single source of truth for what appears in that field.

Here's the catch: if you set value without an onChange handler, the input becomes read-only. The user can click, focus, and attempt to type, but nothing will update on screen. Why? Because React constantly syncs the displayed value with your value prop, and if that prop never changes (no onChange to update state), the input is frozen.

// ❌ Read-only input - you can't type in it
<input value="Hello" />

// ✅ Controlled input - typing works because onChange updates state
function Form() {
  const [text, setText] = useState("Hello");
  
  return (
    <input 
      value={text} 
      onChange={(e) => setText(e.target.value)} 
    />
  );
}

Why This Happens

React re-renders and resets the input's display to match value on every render. If value is hardcoded or never changes, React keeps resetting it back to that fixed value—erasing any typing the user attempts.

Think of it like a thermostat that's locked: the room might try to heat up, but the thermostat keeps forcing it back to the set temperature.

The Controlled Pattern

Pairing value with onChange creates a controlled component: state flows into value, user edits trigger onChange, which updates state, which updates value again. React controls the loop completely.

Key Takeaway: Setting value on an <input> hands full control to React—you must provide onChange to allow user edits, or the input becomes read-only.