I'm using React and I'm trying to map the result of a GET request to the db to display some tabs in my page.
This is my GET request:
useEffect(async ()=>{
const a = await getCategories();
setCategories(a)
},[])
const [categories, setCategories] = useState()
I then store the returned array in my functional component state with useEffect hook:
useEffect(async ()=>{
const a = await getCategories();
setCategories(a)
},[])
const [categories, setCategories] = useState()
At the end, in the render I try to map through the state array like this:
<Nav variant="tabs" >
{categories.map((category, index) => (
<Nav.Item key={index}>
<Nav.Link
eventKey={`link-${index}`}
onClick={() => setCategory(category.category)}
>
{category}
</Nav.Link>
</Nav.Item>
))}
</Nav>
I'm getting a TypeError: Cannot read property 'map' of undefined
I think that's because it runs the map before i have the array stored in the state but i have no idea how to solve it.