Note: I believe someone already asked this question but when I tried looking for it, it suddenly disappeared. If anyone knows where the answer is, can they point me in that direction please? Thanks!
I'm creating a functional component in React using the instructions on https://reactjs.org/docs/faq-ajax.html - where it says "equivalent with Hooks". The only difference is that I'm getting a single item object, not an array.
However, if I try to log the value of isLoaded and item, it seems to appear 3 times in the console:
- false, {}
- true, {}
- true, {object populated now}
So if I try to do the return like the sample says, it will end up inside of the "else" condition after the second iteration of this console logging and if I try to call any properties of that empty object, the page errors out. I have to add another condition to make sure the object is populated before being able to return the JSX, but I'm pretty sure this is the wrong thing to do.
Is there a way to make sure the JSX is only getting returned once rather than these three times, and only returning it when the fetch completes?
I have tried turning this into a Class component but had the same problem. I'd rather not rely on State as there's nothing really dynamic about this page.