React warns about selected on an option element
Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>.Short answer
React does not support the selected attribute on <option>. The choice belongs on the <select>: use defaultValue for an uncontrolled select, or value together with an onChange handler for a controlled one. For multiple selects both must be arrays. Remove selected from every option.
Fix it fast
1Find out which cause you have
Search the project for options that carry selected. Each hit is one source of the warning.
grep -rnE "<option[^>]* selected" src/2Match what you see, then jump to its fix
- 1
<option selected> or <option selected={cond}> in the JSXselected is written on an optionFix - 2
The user cannot change the selection: it snaps back to the old optionA controlled select has value but no onChangeFix - 3
multiple={true} on the select and a string in defaultValue or valueA select with multiple gets a single valueFix
- 1
What does the error look like?
Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>.
Cause 1 of 3
selected is written on an option
1Check
In plain HTML selected marks the initial option. React does not support it on option, whatever value you give it.
grep -rn "selected" src/ | grep option2Fix
Delete selected from the options and give the select a defaultValue (uncontrolled) or a value (controlled).
<select defaultValue="orange">
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select>3Verify
Reload the page: the right option is shown and the warning is gone.
npm run devConfirmed by react.dev
Cause 2 of 3
A controlled select has value but no onChange
1Check
A select that receives a value prop is controlled: React forces the shown option to equal that value. If nothing updates the state on change, the select reverts after every pick.
grep -rn "<select" src/2Fix
Add an onChange that updates the state backing value.
const [fruit, setFruit] = useState('orange');
<select value={fruit} onChange={e => setFruit(e.target.value)}>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select>3Verify
Pick another option: it stays selected and state changes.
console.log(fruit)Confirmed by react.dev
Cause 3 of 3
A select with multiple gets a single value
1Check
With multiple, the initial choice must be an array of strings, and the same is true for value.
grep -rn "multiple" src/2Fix
Pass an array, and read e.target.selectedOptions in onChange.
const [veg, setVeg] = useState(['corn', 'tomato']);
<select
multiple={true}
value={veg}
onChange={e => setVeg([...e.target.selectedOptions].map(o => o.value))}
>
<option value="corn">Corn</option>
<option value="tomato">Tomato</option>
</select>3Verify
Hold Ctrl or Cmd and pick two options: both stay selected.
console.log(veg)Confirmed by react.dev
How do I stop it coming back?
- Keep the selected option in state or in defaultValue, never in the option markup.
- Never switch a select between controlled and uncontrolled: React does not allow it.
- Make every option value match one of the strings you pass as value.
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
React warns that a form field has a value but no onChange handler
You provided a `checked` prop to a form field without an `onChange` handler.
React renders a field as read-only when you give it value (or checked) and no onChange.
5 causes, each with a fixWeb 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 fix