React warns that an input switched from uncontrolled to controlled
Warning: A component is changing an uncontrolled input to be controlled.Short answer
An input is controlled when you pass value. React expects that prop to stay a string for the input's whole life. The warning appears when value is undefined or null on the first render and a string later. Start state as an empty string, or coerce the value with ?? '' when it comes from an API.
Fix it fast
1Find out which cause you have
Print the value passed to the input on the first render. Anything other than a string means that is the cause.
console.log(typeof value, JSON.stringify(value));2Match what you see, then jump to its fix
- 1
The warning appears once, on page load, before you type anythingState is created with no initial valueFix - 2
The warning appears when a form is pre-filled from fetched dataThe value comes from an API and is null or undefined at firstFix - 3
The warning appears after a click, such as Reset or ClearState is cleared to undefined after a reset or a failed loadFix
- 1
What does the error look like?
Warning: A component is changing an uncontrolled input to be controlled.A component is changing an uncontrolled input to be controlled
Cause 1 of 3
State is created with no initial value
1Check
useState() without an argument holds undefined until you set it.
grep -rn "useState()" src/2Fix
Initialise text state with an empty string. For a checkbox the checked prop must always be a boolean.
const [firstName, setFirstName] = useState('');
<input
value={firstName}
onChange={e => setFirstName(e.target.value)}
/>3Verify
Reload and type in the field. The console stays quiet.
npm run devConfirmed by react.dev
Cause 2 of 3
The value comes from an API and is null or undefined at first
1Check
The field is read from an object that is empty until the request finishes, or from a record where the field is null.
console.log(user?.email);2Fix
Coerce the value at render time so the input always gets a string. React's docs show this for values that may be missing.
<input value={user?.email ?? ''} onChange={handleChange} />3Verify
Load the page with a slow network and with a record that has no email.
console.log(typeof (user?.email ?? ''));Confirmed by react.dev
Cause 3 of 3
State is cleared to undefined after a reset or a failed load
1Check
Look for code that sets the state to undefined or null instead of an empty string, or that deletes a key from an object holding the form.
grep -rnE "set[A-Z][A-Za-z]*\((undefined|null)\)" src/2Fix
Reset to the same empty value the state started with, so the type never changes between renders.
function handleReset() {
setFirstName('');
}3Verify
Click the reset control and type again. No warning appears.
npm run devConfirmed by react.dev
How do I stop it coming back?
- Define the initial form state in one object with every field set to '' or false.
- Never pass undefined or null as value; wrap API data with ?? ''.
- Use defaultValue instead of value when you want the browser to own the input.
- Type form state in TypeScript so a possibly undefined field is flagged.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fixWeb apps
next/link throws React.Children.only on its children
Error: React.Children.only expected to receive a single React element child.
React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.
3 causes, each with a fixWeb apps
Hydration failed after adding next-themes dark mode
Console reports a server and client mismatch on the html element
next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.
3 causes, each with a fix