日本語

Fixing the React Warning for selected on option

​​React​HTML,Troubleshooting,TypeScript,Vite

When I built a dropdown in React and added selected to the <option> I wanted selected first, the development console showed a warning that starts with "Use the defaultValue or value props on". If I remove selected and pass the value I want to defaultValue on <select> (or value, if state controls it), the message goes away. You hit this warning when you build a form that should start with one item already selected, such as a sort order or a prefecture picker.

In my test environment, I reproduced several patterns that trigger this message. I copied the console text exactly as it appeared, and I confirmed that it stops appearing after each fix.

Note

The test environment is Ubuntu 24.04 and Node.js 22.23.5. The project was created with the react-ts template from npm create vite@latest (react and react-dom 19.3.1, Vite 8.3.8, TypeScript 7.0.2, oxlint 1.95.0). The components were kept as the template had them, wrapped in StrictMode. I opened the pages by driving Chromium with Playwright 1.63.0. I took the DevTools screenshots with a headed Chromium (not headless) running on a virtual display (Xvfb). I did not test Next.js or other frameworks, and I did not test browsers other than Chromium.

The full console message

I opened the following component on the development server (the same vite process started by npm run dev). The dropdown is for choosing a cat's name. I wanted Tora to be selected first, so I added selected the same way I would in HTML.

tsx
// Wrong: selected is set on the option that should be picked first
export default function SelectedAttr() {
  return (
    <select name="cat">
      <option value="mike">ミケ</option>
      <option value="tora" selected>
        トラ
      </option>
      <option value="kuro">クロ</option>
    </select>
  );
}

The console showed one red line. The message is a warning, but React prints it with console.error, so it appears in the same red as an error.

text
Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>.

It means: "Use the defaultValue or value props on <select> instead of setting selected on <option>."

The DevTools console shows one red line: "Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>." In the top half of the page, a dropdown shows the option Tora (the label is in Japanese) as selected.
The page with selected set on an option. The dropdown shows Tora, and the console shows one error.

The dropdown did show Tora, and the value sent with the form (cat in FormData) was tora. Because the UI looks correct, this warning is easy to miss unless you have the console open.

In React, choose the selected item on the select

In HTML, as MDN's option element page explains, an <option> with the selected attribute is selected from the start. React does not support this approach. The official select reference caveats say that, unlike HTML, React does not support passing selected to <option>. Instead, use <select defaultValue> for an uncontrolled select, or <select value> for a select controlled by state. The option reference gives the same caveat.

I read the development build of react-dom 19.3.1. When the selected value on <option> is neither null nor undefined, it prints this message. The code also still converts the selected value to a boolean and sets it on the DOM option.selected property. That is why Tora was selected in the example above.

Fix: use defaultValue if you only need an initial selection

For an uncontrolled dropdown, remove selected and pass the value of the <option> you want selected to defaultValue on <select>.

tsx
// Fixed: pass the initial value to defaultValue on select
export default function SelectedAttrFixed() {
  return (
    <select name="cat" defaultValue="tora">
      <option value="mike">ミケ</option>
      <option value="tora">トラ</option>
      <option value="kuro">クロ</option>
    </select>
  );
}

After reloading, the console error was gone. The dropdown still showed Tora, and cat in FormData was still tora.

The top half of the page shows a dropdown with Tora (a Japanese label meaning a cat's name) selected. The DevTools console below has only the React DevTools message and no red error line.
The page after switching to defaultValue. The display is the same, and no error appears.

Fix: use value and onChange if the selection is held in state

Sometimes the selected item lives in state, and code needs to change the selection, such as with a "reset to Mike" button. If I computed selected from the state and passed it to each <option>, I got the same message.

tsx
import { useState } from 'react';

const cats = [
  { id: 'mike', name: 'ミケ' },
  { id: 'tora', name: 'トラ' },
  { id: 'kuro', name: 'クロ' },
];

// Wrong: the selected value is computed from state and passed to each option
export default function SelectedFromState() {
  const [catId, setCatId] = useState('tora');
  return (
    <>
      <select onChange={(e) => setCatId(e.target.value)}>
        {cats.map((cat) => (
          <option key={cat.id} value={cat.id} selected={cat.id === catId}>
            {cat.name}
          </option>
        ))}
      </select>
      <p>選んだ猫:{catId}</p>
      <button type="button" onClick={() => setCatId('mike')}>
        ミケに戻す
      </button>
    </>
  );
}

I changed it to match the official example of controlling a select with state. The state goes to value on <select>, and onChange updates the state. Nothing is added to the <option> elements.

tsx
// Fixed: pass state to value on select, and update it in onChange
export default function SelectedFromStateFixed() {
  const [catId, setCatId] = useState('tora');
  return (
    <>
      <select value={catId} onChange={(e) => setCatId(e.target.value)}>
        {cats.map((cat) => (
          <option key={cat.id} value={cat.id}>
            {cat.name}
          </option>
        ))}
      </select>
      <p>選んだ猫:{catId}</p>
      <button type="button" onClick={() => setCatId('mike')}>
        ミケに戻す
      </button>
    </>
  );
}

The cats array and the useState line are the same as before. After this change, the console had zero errors.

In my test environment, the behavior was the same before and after the fix. Both versions opened with Tora selected. Choosing Kuro in the dropdown changed the text below it to show kuro, and clicking the "reset to Mike" button set the dropdown back to Mike.

For multi-select, pass an array to defaultValue

When I added selected to two <option> elements in a <select> with multiple, the same message appeared once. For this case, the official multiple selection section says to pass an array of values to defaultValue.

tsx
// Fixed: for multiple selection, pass an array of values to defaultValue
export default function MultipleSelectedFixed() {
  return (
    <select name="cats" multiple defaultValue={['mike', 'kuro']}>
      <option value="mike">ミケ</option>
      <option value="tora">トラ</option>
      <option value="kuro">クロ</option>
    </select>
  );
}

After the fix, the page opened with Mike and Kuro both selected, and no error appeared.

When the warning does not go away, and other things to watch for

These are the points I ran into in the test environment that were easy to miss.

Writing selected={false} still produced the message. As the code above shows, any value other than null or undefined triggers it. If you add or remove selected based on a condition, you need to remove it from <option> entirely.

When I wrote both <select value="kuro"> and selected on a different <option>, the message appeared, and Kuro, the value passed to value, was selected.

Point

I put two <select> elements with selected on one page, and the message appeared only once. The react-dom code sets a flag after it prints the message once, so it does not print again. Fixing one place may make the message look gone while another one remains. Search for selected across the project, fix every match, and then reload the page to confirm.

Neither the type checker nor the linter catches this. The tsc -b and oxlint (npm run lint) that come with the template reported no errors or warnings, even with selected still in the code. This is because the <option> type in @types/react (OptionHTMLAttributes) includes selected?: boolean.

On a production build (vite build, then opened with vite preview), the page with selected still in place showed no errors or warnings in the console, and Tora was selected on load. This message only appears in development builds, so check it on the development server.

​Posted by 86