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

avatar
Borislav Hadzhiev

Last updated: Apr 7, 2024
3 min

banner

# Warning: Use the defaultValue or value props on <select> instead of setting selected on <option>

The "Warning: Use the defaultValue or value props on <select> instead of setting selected on <option>" occurs when we set the selected prop on an option element.

To resolve the issue, use the value prop instead.

warning user the defaultvalue or value props on select

shell
Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>. at option at select at div

Here is an example of a select element with an initial value set.

App.js
import {useState} from 'react'; const App = () => { const options = [ {value: '', text: '--Choose an option--'}, {value: 'apple', text: 'Apple ๐Ÿ'}, {value: 'banana', text: 'Banana ๐ŸŒ'}, {value: 'kiwi', text: 'Kiwi ๐Ÿฅ'}, ]; const [selected, setSelected] = useState(options[0].value); const handleChange = event => { console.log(event.target.value); setSelected(event.target.value); }; return ( <div> <select value={selected} onChange={handleChange}> {options.map(option => ( <option key={option.value} value={option.value}> {option.text} </option> ))} </select> </div> ); }; export default App;

react select onchange

The code for this article is available on GitHub
Note that we aren't setting the selected prop on the option elements.

Instead, we set the value prop on each option.

Instead of using the selected attribute, to stay consistent with other form elements, React uses a value attribute on the root select tag.

This makes it so <input type='text />, <textarea> and <select> accept a value prop and allow us to implement a controlled component in a very similar way.

When you use the onChange prop, you have a controlled select element, so the defaultValue prop shouldn't be set.

We defined all options in an array to make our JSX code more concise, but you could write each option element manually.

If you'd rather type in the options manually, check out the last code snippet in the article.

We used the useState hook to store the value of the selected option.

The parameter we passed to the hook is the initial state. In the example, we set the initially selected value to an empty string, which is the value of the first option element.

If you want to have a different option selected initially, pass its value to the useState hook.

We set the onChange prop on the select element, so every time its value is changed, the handleChange function is invoked.

App.js
const handleChange = event => { console.log(event.target.value); setSelected(event.target.value); };
The code for this article is available on GitHub

The target property on the event object refers to the select element, so we can access the selected value as event.target.value.

In our handleChange function, we simply update the state with the value of the selected option.

If you need to get the text that's associated with the selected option element, access the label property on the selected option.

App.js
const handleChange = event => { console.log('Label ๐Ÿ‘‰๏ธ', event.target.selectedOptions[0].label); console.log(event.target.value); setSelected(event.target.value); };

I've also written a detailed guide on how to handle the onChange event on a Select element.

# Writing the option elements manually

Here is an example of how you would manually type in the options of the select element without using map().

App.js
import {useState} from 'react'; const App = () => { // ๐Ÿ‘‡๏ธ Initial value of empty string (first option) const [selected, setSelected] = useState(''); const handleChange = event => { console.log('Label ๐Ÿ‘‰๏ธ', event.target.selectedOptions[0].label); console.log(event.target.value); setSelected(event.target.value); }; return ( <div> <select value={selected} onChange={handleChange}> <option value="">--Choose and option--</option> <option value="apple">Apple ๐Ÿ</option> <option value="banana">Banana ๐ŸŒ</option> <option value="kiwi">Kiwi ๐Ÿฅ</option> </select> </div> ); }; export default App;

writing the option elements manually

The code for this article is available on GitHub

We set an initial value of an empty string for the select element, which will render the first option.

You can change which option is selected by default by providing a different initial value, e.g. apple.

Make sure that the initial value is one of the possible values of the value prop on the option elements.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2025 Borislav Hadzhiev