Course contentsShow
← React
Lesson 506 of 2,39614. Uncontrolled ComponentsFree lesson

Common Mistakes: Switching Between Controlled and Uncontrolled

Avoid React warnings by never mixing value/checked with defaultValue/defaultChecked on the same input.

Common Mistakes: Switching Between Controlled and Uncontrolled

What you'll learn: How to avoid React's "switching between controlled and uncontrolled" warning by never mixing value/checked with defaultValue/defaultChecked on the same input.

The Problem: Identity Crisis

React gets very unhappy when you accidentally mix controlled and uncontrolled patterns on the same input. This typically happens when you:

  • Start with value={undefined} (uncontrolled) then later set value="something" (controlled)
  • Use both value and defaultValue on the same element
  • Use both checked and defaultChecked on the same element

Think of it like telling someone "I'll drive" (controlled) and "You drive" (uncontrolled) at the same time. React doesn't know who's in charge!

Common Mistake #1: Undefined Initial State

const [name, setName] = useState(); // undefined!
return <input value={name} onChange={e => setName(e.target.value)} />;

This starts uncontrolled (undefined = no value prop effectively) but becomes controlled once you type. Fix: Initialize with an empty string: useState("")

Common Mistake #2: Mixing Props

// WRONG - both value AND defaultValue
<input value={name} defaultValue="Jane" onChange={...} />

// WRONG - both checked AND defaultChecked
<input type="checkbox" checked={agreed} defaultChecked={false} onChange={...} />

Pick one pattern per input:

  • Controlled: value + onChange (or checked + onChange)
  • Uncontrolled: defaultValue + ref (or defaultChecked + ref)

The Warning You'll See

Warning: A component is changing an uncontrolled input to be controlled...

This means React detected the switch mid-lifecycle.

Key Takeaway: Never use value and defaultValue (or checked and defaultChecked) on the same input, and always initialize controlled state with a defined value (even if it's an empty string).