Design comparison
Solution retrospective
A bug regarding the quote refresh is also still present which I believe has to do with how my setTimeout is written. I think I need a clean up function but I'm not sure where and how to implement it.
For every time you refresh the quote, once the entire app rerenders to change the time, the quote refreshes as many times as you refreshed it in the last ~60s.
If anyone has any insight on this please let me know! The code for this is in src/helpers/useFetchData.jsx
.
If anyone also knows any free apis that allow for more than 150 calls for getting the location that'd also be nice to know. I still have the location hardcoded at the moment.
Thanks in advance!
Community feedback
- @markuslewinPosted over 1 year ago
I agree about the clean up function. What's happening is that a new timer starts every time the data fetching finishes, but the timers from earlier fetches are never stopped. If the refresh button is pressed 3 times, there'll be 3 timers counting down towards the next minute.
clearTimeout
can be used to stop the timers. The function takes an ID of the timer to stop. The ID can be stored in a variable in each effect:useEffect(() => { let timeoutId; Promise.all([getTime(), getPlace(), getQuote()]) .then((results) => { // state stuff timeoutId = setTimeout(() => { setRefresh(!refresh); }, timeout); }) .catch((err) => err); return () => { clearTimeout(timeoutId); }; }, [refresh]);
There's usually another problem related to data fetching like this. When multiple fetches are in-flight, there's risk for a race condition if one of the earlier requests resolves after the latest request resolves. I couldn't reproduce that in this project, though. I think it's because
refresh
sort of works like a lock that ensures only one fetch is in-flight at any given time. Maybe that's by design?!I'll post a link to the data fetch example in the docs, just in case. That whole page is great:
https://react.dev/learn/you-might-not-need-an-effect#fetching-data
Marked as helpful1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord