0

So I am creating an app using the REST countries API and I have a useEffect hook that runs on the empty dependency array and I am expecting it to run once and I am trying to append to my array all the types of currencies associated with the country but for some reason the useEffect runs twice and appends the same currency twice to my array and I would like to know why that is. Any help is much appreciated.

my code:

useEffect(() => {
       axios.get(`https://restcountries.com/v3.1/name/${countryName}`).then(response => {
        console.log(response.data[0])
        setData({
            name: countryName,
            image: response.data[0].flags.png,
            nativeName: response.data[0].name.nativeName.nld,
            population: response.data[0].population,
            region: response.data[0].region,
            subRegion: response.data[0].subregion,
            capital: response.data[0].capital[0],
            borderCountries: response.data[0].borders,
            topLevelDomain: response.data[0].tld[0],
            currencies: response.data[0].currencies,
            languages: response.data[0].languages,
        })
        
        for (let k in response.data[0].currencies) {
            setCurrencies(prev => [...prev, k])
        }
       }) 
    },[])

The output

Fernando
  • 29
  • 7

0 Answers0