So I am creating an app that utilizes the REST countries API and I am trying to call to the API on the first render and from my understanding, in order to do this you have to use an empty array in the useEffect function as such
const LightMode = () => {
const data = useRef([])
useEffect(() =>{
axios.get('https://restcountries.com/v3.1/all').then(res=>{
res.data.forEach(country =>{
//console.log(country)
data.current.push({
name: country.name.common,
population: country.population,
region: country.region,
capital: country.capital,
image: country.coatOfArms.png
})
})
})
}, [])
console.log(data)
return(
<div>
<NavigationBar />
<div className='temp'>
<Card className='country-cards'>
<Card.Img variant='top' src={data[0].image}/>
<Card.Body>
<Card.Title></Card.Title>
</Card.Body>
</Card>
</div>
</div>
)
}
but when I run the app I get an error saying unable to read undefined so the first render technically never runs? I want to know why that is. I am still learning how first renders work so any help is much appreciated, also if there is any more information needed let me know.
