I'm trying to populate a dropdown (specifically a FormControl as select since I'm using React Bootstrap) with some data I'm retrieving from my backend (in my case some names). The structure of the data is
{
data: [
{name: "name1"},
{name: "name2"},
...
]
}
I can retrieve the data since I can map the result and put every name in an array but my dropdown is always empty. What am I doing wrong? this is my code:
useEffect(() => {
fetch(`some/url`)
.then(rsp => rsp.json())
.then(data => namesArray = data.data.map(el => { //data.data is correct, I'm sending the json this way
return el.name;);
});
return(
...
<Row>
<Form.Label column sm={4} md={{ offset: 1 }} className="font-weight-bold">
Names
</Form.Label>
<Col sm={8}>
<FormControl
size="sm"
id="names-dropdown-"
as="select"
value={model}
onChange={e => {
setNames(e.target.value)
}}
>
{namesArray.map(el => (<option value={el}>{el}</option>))}
</FormControl>
</Col>
</Row>
...)