I have a <select>
tag. The options for this tag are contained in an array. The values of this array are the fields I want to show to the user, and the index is the value I want to provide to my "onChange" function. So far, so good.
However, when I open the popup containing the <select>
, the default value is not the value I set.
I have tried setting defaultValue
, value
and I of course checked the value I wanted as default.
Here is a sample of what I mean:
options = ["string1", "string2", "string3", ...];
valueIWant = options[indexGivenToFunction];
(...)
<select name="content" defaultValue={valueIWant} onChange={this.changeContent}>
{ options.map((opt, index) => {
return (
<option value={ index } key={ opt }>{ opt }</option>
);
})}
</select>
I would like the defaultValue when opening my popup to be the one I set here. However, it seems to be set to the first value of my array.
A (very) wild guess I'm making is that because select doesn't allow any other fields than the ones it provides, and because when I set the defaultValue options.map
isn't resolved yet, it ignores the value I give it, and instead takes the first option as entry for this field (as I said, just a guess). Am I right? And is there a way to bypass this behaviour to be able to set a defaultValue anyway?
Thanks for any help.