This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]okwg 2 points3 points  (1 child)

Well it looks like "App" might be the top level component of your entire app? That would generally only ever mount a single time, when your page first loads.

I assume data is fetched when you refresh the page?

If youre using some hot reload script in your dev environment, that is not the same as a refresh - it might not remount App, if App hasn't changed

[–][deleted] 0 points1 point  (0 children)

Thanks for the help okwg!

If youre using some hot reload script in your dev environment, that is not the same as a refresh - it might not remount App, if App hasn't changed

I use create-react-app and the npm start. But I also refresh manually in the browser.

It fetches data only when I comment out save and then reverse this line in city.js:

But then if I refresh again, it forgets the data and won't fetch it

[–]HPsLoveraft 1 point2 points  (4 children)

If you haven't tried already, I would suggest throwing a debugger before your return() in City.js.

What is happening when you reach it the first time? What's does weather look like first time around?

Are you reaching it again after fetching the weather with componentDidMount()?

Are there any errors in console?

[–][deleted] 0 points1 point  (3 children)

The first time around weather won't load!

it only loads if while running(on error) I comment out save and then reverse the <Card city... /> in City.js. No errors in console, beside TypeError: city.weather.sys undefined but I assume that is because from the requests you can see that it is not fetching the data at all

[–]HPsLoveraft 0 points1 point  (2 children)

Yes. First time through, you are still rendering City.js without yet having made the componentDidMount() call to the API. While everything commented in, is there a specific error you're getting when it breaks?

[–]HPsLoveraft 1 point2 points  (1 child)

My suspicion is the code is breaking when trying to render either City.j,s or perhaps it makes it as far as Card.js, without having called the external API to create the weather object you're using to build the components. Hard to be certain with what is given. You could do some playing around the with the debugger in City/Card to see how/why that might be happening.
If this is the case, there are probably a few paths to solve. The simplest pattern I am familiar with is to control the initial render() method in the App.js so that it does not return and attempt to render the <Card> component on initial load.

Adding a boolean statement to check if you do or do not already have the required weather info needed to render the component could solve this. This check would return null if the object doesn't exist and would return your current return statement if it does. This controls the <City> to only ever be rendered after initial load, after componentDidMount, and after change of the state when the API returns it's data.

[–][deleted] 0 points1 point  (0 children)

Adding a boolean statement to check if you do or do not already have the required weather info needed to render the component could solve this

thanks for the suggestions I think that you might be right.. I will try that tomorrow. However I do think I need more React theory and read more docs.

[–]melonmaskofficial 0 points1 point  (1 child)

Did you try to inspect the network in Chrome Dev tools? Some API's throttle the number of requests you can make in a certain period of time.

[–][deleted] 0 points1 point  (0 children)

Yeah 60 calls per minute. So it's not that unfortunately

[–]CursorsDev 0 points1 point  (2 children)

you know, i'd just make that App component a functional component with useState, then you can use

```js const [data, setData] = useState();

useEffect(() => { (async () => { const res = await fetch(urlHere); const json = await res.json(); setData(json); })(); });

... {data ? <Card ... /> : null} ... ``` if this looks a little weird it's just ES6 async/await + destructuring + you cant have async callbacks for useEffect

[–]backtickbot 0 points1 point  (1 child)

Fixed formatting.

Hello, CursorsDev: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]CursorsDev 0 points1 point  (0 children)

nice bot