Course contentsShow
← React
Lesson 1200 of 2,39631. Forms in ReactFree lesson

Mixing Controlled and Uncontrolled: Anti-Pattern Warning

Understanding why switching between controlled and uncontrolled modes for the same input causes React errors.

Mixing Controlled and Uncontrolled: Anti-Pattern Warning

What you'll learn: Why switching an input between controlled and uncontrolled modes triggers React errors and breaks your form behavior.

The Problem: Flip-Flopping Control

When an input starts with value={undefined} (uncontrolled) and later switches to value={someString} (controlled)—or vice versa—React throws a warning. This happens because React needs to know at mount time who's managing the input: the DOM or your component state.

Think of it like changing the rules mid-game: if you start letting the DOM track the input value, then suddenly try to control it with state, React doesn't know which source of truth to trust. The input can behave unpredictably—losing cursor position, ignoring updates, or displaying stale values.

Common Causes

1. Conditionally controlled value:

<input value={user?.name} /> // undefined → string when user loads

2. Starting with null:

const [text, setText] = useState(null);
<input value={text} /> // null is uncontrolled!

3. Dynamic controlled/uncontrolled switching:

<input value={editMode ? text : undefined} />

The Fix: Stay Consistent

Always initialize controlled inputs with a string (even "") and never switch to undefined/null:

const [name, setName] = useState(""); // Start with empty string
<input value={name} onChange={e => setName(e.target.value)} />

If you need conditional behavior, keep it controlled but change the value:

<input value={editMode ? text : ""} /> // Always controlled

Key Takeaway: An input must be either controlled or uncontrolled for its entire lifecycle—never switch between modes by toggling between defined and undefined value props.