In my react app, I am retrieving some data from firebase , but when I try to store the items in my state it stores only the first item of the fulfilled promises ,
const HomePage = (props) => {
const [events ,setEvents] = React.useState([])
const fetchGames=async()=>{
const DB =await db.collection('game').get()
DB.docs.forEach(item=>{
setEvents([...events,item.data()])
console.log(item.data()); // Here shows all the items that stored in my firebase collection
})
}
useEffect(()=>
{
fetchGames()
}, [])
return(
<div>
{
events.map((event)=>
(
<div>
{event.home_color}
</div>
)
)
}
</div>
)
Eventually the events state will display only one item , unlike in the console log which displayed all the items , how to fix this?