← Back to React, page 6

react-0103 · Easy fresher

Why does React warn "A component is changing an uncontrolled input to be controlled", and why is a file input always uncontrolled?

React decides whether an input is controlled by checking whether the value prop is undefined on the first render. If it starts undefined (uncontrolled) and later becomes a string (controlled), React warns because the two modes behave differently. The fix is to give the state an initial empty string or use value={x ?? ''}. A file input is always uncontrolled because its value can only be set by the user, so you read it with an event or a ref.

Full answer

A controlled input gets its value from React state through the value prop and reports changes with onChange. An uncontrolled input keeps its own value in the DOM, and you read it when needed. React picks the mode by looking at value: if it is not undefined, the input is controlled.

The warning appears when this changes during the lifetime of the input. A typical cause is const [name, setName] = useState() with <input value={name} />. On the first render the value is undefined, so the input is uncontrolled. After the user types, state becomes a string, so React switches modes. The reverse switch (controlled to uncontrolled) happens when state becomes undefined or null, for example after resetting with setName(undefined) or reading a field missing from API data.

Why it matters. In controlled mode, React owns the value and resets the DOM to match state after every render. In uncontrolled mode the DOM owns it. Switching in the middle can lose text or leave the DOM and state different. React warns so you pick one mode.

Fixes. Initialize state with an empty string: useState(''). When the value may come from data that can be missing, normalize it: value={user.name ?? ''}. For checkboxes use checked with a boolean, and pass onChange whenever you pass value, or React warns about a read-only field.

File inputs. <input type="file" /> cannot be controlled, because browsers do not allow code to set the selected file for security reasons (only the user can choose). You read the files from the change event (event.target.files) or from a ref, and keep what you need (the File objects) in state. You may reset it by changing the key or by resetting the form.

When to use uncontrolled on purpose: simple forms read on submit through FormData, or inputs managed by a form library.

The example shows a safe controlled field and a file input read from the change event.

jsx
import { useState } from 'react';

export default function ProfileForm({ user = {} }) {
  const [name, setName] = useState(user.name ?? '');
  const [fileName, setFileName] = useState('no file chosen');

  return (
    <form>
      <label>
        Name
        <input value={name} onChange={(e) => setName(e.target.value)} />
      </label>
      <label>
        Photo
        <input type="file" onChange={(e) => setFileName(e.target.files[0]?.name ?? 'no file chosen')} />
      </label>
      <p>
        Hello {name || 'stranger'}, file: {fileName}
      </p>
    </form>
  );
}

The user prop is missing here, so user.name is undefined and the nullish default keeps the input controlled from the first render. The check typed a name. The warning itself appears in the browser console when the default is left out. Rendered result: <form><label>Name<input value="Asha"></label><label>Photo<input type="file"></label><p>Hello Asha, file: no file chosen</p></form>

Key points

  • Controlled means value is not undefined. Starting undefined and becoming a string triggers the warning.
  • Initialize with an empty string or normalize missing data with ?? ''.
  • Passing value without onChange creates a read-only field and a warning.
  • A file input is always uncontrolled because code cannot set the selected file.
  • Read file inputs through onChange or a ref, and store the File objects you need.

Common mistakes

  • Calling useState() with no initial value for a controlled input.
  • Passing null from an API directly to value.
  • Passing value without onChange.
  • Trying to set the value of a file input from state.
  • Switching between value and defaultValue on the same input.

Follow-ups

  • What is the difference between defaultValue and value?
  • How would you clear a file input after upload?
  • When is an uncontrolled form with FormData the better choice?

Tips

  • Explain how React decides the mode (value is undefined or not).
  • Give the nullish fallback fix. It is a one-line answer.
  • Mention the browser security reason for file inputs.