Search code examples
javascriptdestructuring

React Native - OpenWeatherMap : Declaration with the same name


Hello everyone !

I am a student creating a "weather" tab for a mobile application project.

I am using the OpenWheatherMap API, "One Call API" precisely and I have a small problem. To receive the information from the API here is what I do:

1.WeatherScreen.js

2.WeatherInfo.js WeatherScreen

WeatherInfo

And this is the error I got (I know the meaning of it but don't know how to bypass it) : Error

If someone can indicate me what to do it will be awesome !

Thanks in advance and good day / good night !


Solution

  • You are destructuring currentWeather and you have temp in both current and daily and you have icon in both weather and daily. Destructuring is fine but you need to alias those fields so you don't end up with re-declaration of temp and icon.

    Here is an example:

    const { current: { temp: currentTemp, humidity, wind_speed }, 
      // etc.
      daily: { temp, icon },
      // etc.
    }
    

    This way, temp variable is not duplicated and will point to daily temperature and currentTemp will point to current temperature.