Home › JavaScript

Warning: A component is changing an uncontrolled input to be controlled. (JavaScript) — how to fix

Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component.

Quick fix: Initialize state with an empty string instead of undefined: useState(""), not useState(). For possibly-missing data use value={name ?? ""}, and for checkboxes checked={!!flag}. Or make it uncontrolled with defaultValue and no value.

What it means

React has two kinds of form fields:

value={undefined} and value={null} mean "uncontrolled". When that later becomes a string, React warns because the field switched modes mid-life. The reverse message, changing a controlled input to be uncontrolled, means a defined value became undefined.

Cause 1: state initialised without a value

const [email, setEmail] = useState();        // undefined
<input value={email} onChange={e => setEmail(e.target.value)} />

Fix: start with an empty string:

const [email, setEmail] = useState("");

For checkboxes start with a boolean: useState(false).

Cause 2: form object with missing fields

const [form, setForm] = useState({});
<input name="city" value={form.city} onChange={handleChange} />

form.city is undefined until the user types. Declare every field up front:

const [form, setForm] = useState({ name: "", email: "", city: "" });

const handleChange = e =>
  setForm(f => ({ ...f, [e.target.name]: e.target.value }));

A bad setForm({ [e.target.name]: e.target.value }) (without spreading ...f) drops the other fields and triggers the reverse warning.

Cause 3: data loaded from an API

Fields from a fetch are undefined before loading and null for empty database columns:

<input value={user?.phone ?? ""} onChange={...} />
<input type="checkbox" checked={!!user?.subscribed} onChange={...} />

Use ?? (not ||) if 0 is a valid value for number inputs.

Alternatively render the form only after the data arrives: if (!user) return <Spinner />;

Cause 4: you actually want an uncontrolled field

If you don't need the value on every keystroke, drop value and use defaultValue, then read it on submit:

<form onSubmit={e => {
  e.preventDefault();
  const data = new FormData(e.currentTarget);
  console.log(data.get("email"));
}}>
  <input name="email" defaultValue="" />
</form>

Note defaultValue is only read on first render; changing it later does nothing (change the key to reset the field).

Libraries

Last updated 2026-11-22.