The code below returns "TypeError: options.map is not a function" which I'm assuming is happening because it's not an array but a promise object as mentioned in the title. Code below:
function DownloadMenus() {
async function grabIDs(){
const response = await fetch("http://localhost:5000/obviusmetersID");
const jsonData = await response.json();
let ids = [];
for(var i = 0; i< jsonData.length; i++){
var obj = jsonData[i];
ids.push(obj.id);
}
return ids;
}
const[options, setOptionsData] = React.useState(grabIDs);
const [selectedIndex, setSelectedIndex] = React.useState(0);
const handleMenuItemClick = (event, index) => {
setSelectedIndex(index);
setOpen(false);
};
...
...
return(
...
...
<ClickAwayListener onClickAway={handleClose}>
<MenuList id="split-button-menu">
{options.map((option, index) => (
<MenuItem
key={option}
disabled={index === 2}
selected={index === selectedIndex}
onClick={(event) => handleMenuItemClick(event, index)}
>
hi
{option}
</MenuItem>
))}
</MenuList>
</ClickAwayListener>
I tried to do the following:
<MenuList id="split-button-menu">
{async () => {await options.map((option, index) => (
<MenuItem
key={option}
disabled={index === 2}
selected={index === selectedIndex}
onClick={(event) => handleMenuItemClick(event, index)}
>
hi
{option}
</MenuItem>
))}}
</MenuList>
But when I do that, I get a warning saying I have an invalid prop type and nothing appears in the listing.
I want to assume that I'm missing a step that allows the promise to resolve before being passed but I'm not sure where exactly to put that in.
I can edit the code blocks as necessary but I think these are all the relevant pieces.