Skip to content

Warning: A component is changing a controlled input to be uncontrolled

Error message

                A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined.
              

Quick summary

An input's `value` prop changed from a defined value to `undefined` (or null), switching it from controlled to uncontrolled.

Why this happens

  • State was set to undefined or null at some point

  • A prop that feeds the input value became undefined

  • Conditional logic sets value to undefined in some cases

Minimal example

✗ Broken code
const [value, setValue] = useState(); // undefined!
return <input value={value} onChange={e => setValue(e.target.value)} />;
✓ Fixed code
const [value, setValue] = useState(""); // Empty string
return <input value={value} onChange={e => setValue(e.target.value)} />;

Initializing with '' instead of undefined keeps the input controlled at all times.

How to diagnose

  • Check if the state value can become undefined or null

  • Log the value prop to see when it changes to undefined

  • Look for conditional assignments that might set undefined

How to fix

  • Initialize state with an empty string: useState('') not useState()

  • Provide a fallback: value={text || ''}

  • Ensure the value is always a string, never undefined

How to prevent

  • Always initialize string state with ''

  • Use default prop values for inputs

  • Never set state to undefined for controlled inputs

Related Resources

Related Lessons

  • Forms

    Learn controlled vs uncontrolled inputs.

← Back to language