Skip to content
Advertisement

Selected option not persisting – React

Have a simple drop down list being populated from an axios request. It is working outside of the fact that the actual selected item in the “Select a team” drop down does not persist when selected and the default value “Select a team” continues to show. What am I missing here, any help is greatly appreciated!

JavaScript

Adding a screenshot for referance. Here you can see that the manager “Dan” is selected yet his name doesn’t appear in the input.

Image showing an example

Advertisement

Answer

You do not explicitly mark as selected any option, and since you do key={uuidv4()} for each one, it means than on each re-render the options are considered different that then previous render, and so do not keep their default highlight.

You can provide a value prop to the select element and that will take care of highlighting the correct option.

JavaScript

Initial answer (below) is wrong for react.

You will need to mark the selected one with

JavaScript
Advertisement