Design comparison
Solution retrospective
I decided to use fetch to make the API call, but whenever I ran it in Firefox, the call got cached and would return the same result over and over again. It works as expected in Chrome. Does anyone know what would cause this in Firefox? Also if anyone has tips on centering the button on the container div, I would really appreciate it.
Community feedback
- @PatrickLee22Posted over 1 year ago
Hello there 👋. Congrats on completing the challenge!
To resolve the caching issue, you can set the cache option in your fetch call to 'no-cache' to prevent the browser from caching the response, like so:
fetch('https://api.adviceslip.com/advice', {cache: "no-cache"})
Alternatively, the Advice Slip API has an option to get a specific advice slip if you provide an id number at the end of the URL. You could append a random number to the URL and it which would bypass the caching on the server side.
Marked as helpful1@CMac450Posted over 1 year ago@PatrickLee22 Thank you for the explanation! Both are great solutions.
0@ifarontiPosted over 1 year ago@PatrickLee22
I tried marking your suggestion as helping but I wasn't able to. your no cache method is golden. Thanks!!
0@PatrickLee22Posted over 1 year ago@ifaronti I appreciate the thought regardless. Glad I could help.
0 - @asbhogalPosted over 1 year ago
Hi Cassidy,
Great job! I've looked at your code and made some suggestions below:
- You should ideally use the
useEffect
hook to fetch the API data when the component mounts (ie. on first render) and use the setter function in theuseState
to pass the response object to the variable. Initially this should be set to null and dynamically updated. Then you can call the API using the function name which is inside the hook. Here's a link explaining this in further detail Link - You should also implement the
.catch()
method with theasync/await
to catch any errors that could be thrown during the API call. Conventionally you would return these to the console.log. Here's a link from the MDN website explaining this in further detail Link - Also, to center the
button
vertically you'll need to take it out of the natural flow by using a declaration liketransform: translateY(35px)
for example. Another thing - yourbutton
shouldn't be wrapped in adiv
as this makes it difficult for keyboard users and other accessibility-based assistive technologies to be able to access it.
Hope this help!
Marked as helpful0@CMac450Posted over 1 year ago@asbhogal Thank you Aman, your explanations and links are much appreciated!
1 - You should ideally use the
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