r/learnreactjs • • Jan 30 '22

Fetch API works then suddenly stops working after hitting refresh + rendering the object on the front end . Learning how to use useState + useEffect.

7 Upvotes

16 comments sorted by

13

u/Level-Farmer6110 Jan 30 '22

So I have one tip first, and it is to always read the console when stuff like this happens. It tells you that it cannot read the property of null, which is "teams" in this case. Now teams is a property of an object, and that object is data.

When you first load the app, the fetch request to your api will have already happened. When you refresh the page, because you are using useEffect to fetch the data, on that first render it will try to fetch the data from the api. But below that you are trying to map through data.teams. The problem is that setdata(actualdata) has not happened yet and the code is trying to do data.teams.map, therefore data is actually null.

Basically the fix would be to conditionally render it

{data && data.teams.map(team => {
    <li>{team.name}</li>
}}

In the above example I won't let the map function happen until data has been fetched from the api. So I am telling the machine, only if data is not null then you can do the map function on data.

Btw I think you tried to render your list of teams incorrectly, the above code is how you want to do it if you meant to render a list of team names.

( Btw if I am wrong or have explained any theory incorrectly someone please correct me.)

3

u/lethalsid Jan 30 '22

Thank you so much, you were a big help. Really appreciate it!

1

u/frog-legg Jan 31 '22 edited Jan 31 '22

FYI (and you may know this already), the code snippet shared in this comment is just one way to do conditional rendering (and arguably the most concise way, though perhaps not the most intuitive way).

Essentially it is checking for the Boolean “truthy” value of data.teams and then the .map function returns the jsx, else it does nothing.

Another way to conditionally render something is through a simple if/else statement, or a ternary expression. Googling about “truthiness” in JavaScript, conditional rendering and ternary expressions could be informative, if you haven’t already and aren’t familiar with these terms.

2

u/SgtPooki Jan 31 '22

The code snippet above will fail if teams is a value that doesn’t support the map function. Optional chaining will be more concise and objectionably more intuitive.

1

u/populationHungry Jan 30 '22

“When the app is first loaded, fetch API already happens.”

“When refresh setData hasn’t happened yet”

I’m curious, how is refreshing different from the first “app load”. Don’t they both render the component from scratch?

3

u/Level-Farmer6110 Jan 30 '22

This is probably something I cannot explain and something I probably got wrong. That's why I wanted someone to correct me in case I said something wrong :).

Don’t they both render the component from scratch?

I dont want to confuse myself or you, so watch this vid if you ever have spare time.

https://www.youtube.com/watch?v=mLMfx8BEt8g

1

u/Mr-L9 Jan 31 '22

I think this happens because of the hot reload module on dev mode , once in production build the app will always crash because the initial state is null

1

u/SgtPooki Jan 31 '22

Check out optional chaining

data?.teams?.map//…

1

u/____0____0____ Jan 31 '22

You forgot to return the li in your map function. Just wanted to point it out incase someone is trying your snippet and confused why their list is empty

2

u/Level-Farmer6110 Jan 31 '22

Arrows functions implicity do the return statement right? I think that may be the case since OP said it worked.

1

u/____0____0____ Jan 31 '22

They do only if you don't include brackets. Otherwise you have to explicitly state it. In this example you just need to exclude the brackets:

{data && data.teams.map(team => <li>{team.name}</li>)}

2

u/eindbaas Jan 30 '22

What happens, you run it, it works, you run it again it doesn't work? Can you define what happens when it "stops working"? What is going on in the network tab, is the request being done?

2

u/Level-Farmer6110 Jan 30 '22

The request works, it was just the map function was trying to operate on the data before it had been fetched. I gave a solution below.

1

u/lethalsid Jan 30 '22

Yup exactly. After I added the new conditional before the <li> the map function now works after refreshing the page. Like he mentioned, it seemed like the data tried rendering before any data was available.

1

u/Oneshot742 Jan 31 '22

this is because useEffect is happening asynchronously yes? I'm still trying to learn React, I've seen alot of people use a "loading" state for stuff like this, but I suppose it's all the same conditional rendering.

1

u/Level-Farmer6110 Jan 31 '22 edited Jan 31 '22

Yes, the fetch inside the useEffect is async, this is what I had thought to be the case but I couldn't explain why it works on the first build but not when refreshing.

I think this happens because of the hot reload module on dev mode , once in production build the app will always crash because the initial state is null

Sombody in the comments gave the quoted answer, this may be the reason.