Skip to content
>_VibeUnstuck

A component is changing an uncontrolled input to be controlled

Last verified:

The fix

An input's value started as undefined and later became a string. React will not switch an input from uncontrolled to controlled. Pass a string for the whole lifetime of the field.

  1. 1

    Find the value that is undefined on the first render

    The warning names the component. In that component, every value or checked prop is a suspect. useState() with no argument, value={user?.name}, and value={data && data.title} all pass undefined or null at least once. A shadcn Input forwards value to the DOM input, so the warning still comes from your prop.

  2. 2

    Keep a string in value for the field's whole life

    The input docs' controlled example initializes state to '' and sets it from onChange. If the string arrives later from an API, pass value={name ?? ''}. Do not pass value={undefined} to mean "empty." For a checkbox, keep checked a boolean: checked={on} with useState(false), not checked={maybe}.

    const [name, setName] = useState("");
    
    <input
      value={name}
      onChange={(event) => setName(event.target.value)}
    />
  3. 3

    Use defaultValue only if you mean uncontrolled

    An uncontrolled input takes defaultValue and no value prop. React will not track it after the first render. Do not pass both value and defaultValue. Do not delete onChange to quiet the warning. Without onChange, a controlled input snaps back and looks frozen.

  4. 4

    Stop the value changing back to undefined

    If the warning is the reverse, a defined value became undefined. Clear with setName(''), not setName(undefined). When a loaded record is replaced by null, the input should get '', not the missing field. Confirm the warning is gone by typing in the field after the data loads.

Why it happens

React's input docs state the warning as A component is changing an uncontrolled input to be controlled. The rest of the sentence says this is likely caused by the value changing from undefined to a defined value, which should not happen, and that you should decide between a controlled and an uncontrolled input for the lifetime of the component. The same page's troubleshooting section says you cannot pass value={undefined} first and later pass value="some string". A controlled input has to receive a string value the whole time, not null or undefined.

Generated forms do this by writing useState() with no initial value, or value={user?.name} while user is still loading. The first render passes undefined, so the input is uncontrolled. The next render passes a string, and React warns. The reverse warning, A component is changing a controlled input to be uncontrolled, is the string later becoming undefined again, often when a clear button does setName(undefined) or a field is value={data?.title} after data is cleared.

The input docs also say an input cannot be both at once, and a controlled input needs an onChange that updates the value synchronously. Passing value without onChange does not throw this warning. It makes the field refuse keystrokes, because React puts the old value back. Checkboxes use checked, and that prop has to stay a boolean.

The next error you'll probably hit

Once this is fixed, the usual follow-up is Too many re-renders. React limits the number of renders to prevent an infinite loop.. A state update ran during render and immediately scheduled another render. React stopped it. A production build reports this as minified React error #301.

Prompt to paste back into your AI tool

Paste this above the error.

React warns: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value. If the warning says controlled to uncontrolled, say that instead. The component is:
<paste the form component>

Do not remove the value prop and leave the field uncontrolled if this form is supposed to be controlled. Do not pass both value and defaultValue. Do not set state to undefined to clear the field. Do not delete onChange.

1. Find the value or checked prop that is undefined or null on the first render.
2. Initialize useState to '' for text (false for a checkbox), or pass value={name ?? ''}. Update it in onChange.
3. If the warning is the reverse, stop assigning undefined. Clear with an empty string.
4. Change only that input and its state.

Common questions

+The field shows the warning and I also cannot type in it. Which bug is that?

The warning is value flipping between undefined and a string. A field that ignores keystrokes is a controlled value with no onChange, so React resets it. Fix the undefined value, and keep onChange writing into that state.

+Does defaultValue fix a controlled form?

defaultValue makes the input uncontrolled. React uses it only for the initial paint and then ignores later changes to your state. Use it when you do not need React to hold the text. A form you submit from state needs value as a string and onChange.

+Why does a checkbox say the same thing when value is a string?

A checkbox is controlled with checked, a boolean, not with value. The input docs say checked has to stay a boolean for the lifetime of the box. useState(false) and checked={on} does that. value on a checkbox is the form payload, not the on/off state.

Related errors