I am at beginner level of React framework and am creating a weather app. So I fetch the data from the API and pass the object as a value to a parameter in my component. Now when trying to access the inner data values of the object, I am facing an error.
So I took the parameter as in App.js
{weather && <WeatherDisp data = {weather}/>}
in WeatherDisp.js
const WeatherDisp = ({data}) => {
return (
<div>
<p>{data.main.temp}</p>
</div>
);
}
on writing this I am getting the error as :
Cannot read properties of undefined (reading 'temp')
Can anyone help?
It would be great if you used loading while waiting for the API data response, and then rendered your component. Additionally, you can include checks to see if the data exists in the object or not. Please try the code below, as it should be helpful.
or better way as below