Skip to content
Advertisement

React API not showing the data

I am not able to retrieve content from API every time I reload my page it shows error, please see the attached image, I wanted to find the weather details using Weather API and right now I am using static latitude and longitude.

JavaScript

https://i.stack.imgur.com/oqr7i.jpg

Advertisement

Answer

The problem with your code is that you’re trying to render {weatherData2.current.temp} before the data is returned from the weather API and that’s why your weatherData2 will be undefined while rendering.

You can add a loading state for checking if the data is rendering or already rendered.

You can try below code:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement