Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React warns that an input switched from uncontrolled to controlled

ErrorWarning: 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.

  • Applies to React 19, Next.js 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
  2. 2Match what you see, then jump to its fix

    1. 1The warning appears once, on page load, before you type anythingState is created with no initial valueFix
    2. 2The warning appears when a form is pre-filled from fetched dataThe value comes from an API and is null or undefined at firstFix
    3. 3The warning appears after a click, such as Reset or ClearState is cleared to undefined after a reset or a failed loadFix

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 dev

Confirmed by react.dev

Did this fix it?

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

Did this fix it?

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 dev

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.