Skip to content
Advertisement

How to stop a component from setting state before the data fetching?

I have a page and that page has two components. I am fetching the data from root component and passing data to child component using react-redux. I tried to set the fetched data to a state variable that is in child component but it sets data before the fetch is complete in the root component. So, I am getting blank object.

JavaScript
JavaScript

You can check the whole codebase : https://github.com/Sadiq1029/weather

Can someone help?

Advertisement

Answer

Check if data is not null then update your child state

JavaScript

data.ts

JavaScript

Background.tsx

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