Maaz Zindani

Loading the index…

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

React warns about selected on an option element

ErrorUse 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.

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

Fix it fast

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

    1. 1<option selected> or <option selected={cond}> in the JSXselected is written on an optionFix
    2. 2The user cannot change the selection: it snaps back to the old optionA controlled select has value but no onChangeFix
    3. 3multiple={true} on the select and a string in defaultValue or valueA select with multiple gets a single valueFix

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 option

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

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

Next step

Want this handled for good?

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