The defaultValue Trap in Controlled Components
What you'll learn: Why using defaultValue on a controlled input breaks React's data flow and how to prevent this common mistake.
The Trap Explained
When you control an input with value={state}, React expects to be the single source of truth for that input. The defaultValue prop, however, is designed for uncontrolled components—it tells the DOM what initial value to use before letting the DOM take over management.
Here's the trap: if you write <input value={name} defaultValue="John" />, React will ignore the defaultValue entirely. Worse, developers sometimes use defaultValue thinking it will handle the initial render while value handles updates. This creates confusion because defaultValue does nothing in controlled mode.
Why It Breaks the Contract
A controlled component means React owns the value at all times, including the initial render. Using defaultValue suggests you want the DOM to have initial control, which contradicts the controlled pattern.
Think of it like a TV remote: Either you (React) control the channel at all times (controlled), or you set an initial channel and let someone else change it freely (uncontrolled). You can't do both simultaneously.
The Correct Approach
For controlled inputs, initialize your state with the desired starting value:
const [name, setName] = useState("John"); // Initialize state
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
No defaultValue needed—your state is the initial value.
For uncontrolled inputs, use defaultValue and skip the value prop entirely.
Key Takeaway: In controlled components, never use defaultValue—initialize your state instead. The value prop handles both initial and subsequent values when React is in control.