I am trying to populate a select component from Material UI with data from my database. I can successfully show the data in the component, but when I select one option it breaks and the following error "categorias.map is not a function" shows up. Does anybody know what am I doing wrong? Here is the code:
<FormControl className={classes.formControl}>
<InputLabel id="demo-simple-select-label">Categorias</InputLabel>
<Select
labelId="demo-simple-select-filled-label"
id="demo-simple-select-filled"
value={categorias}
onChange={(ev) => setCategorias(ev.target.value)}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
{categorias.map((categoria) =>
<MenuItem value={categoria.id}>{categoria.nombre}</MenuItem>
)}
</Select>
</FormControl>
Here is more code regarding categorias:
const [categorias, setCategorias] = useState([]);
useEffect(() => {
const getCategorias = async () => {
const res = await fetch("/categories", {
method: 'GET',
headers: {'Content-Type': 'application/json'},
})
//console.log(res);
const response = await res.json();
setCategorias(response);
console.log(categorias);
}
getCategorias();
})
Thank you!