Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

React Advice Generator

P
Cassidy 160

@CMac450

Desktop design screenshot for the Advice generator app coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
  • API
2junior
View challenge

Design comparison


SolutionDesign

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

@PatrickLee22

Posted

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 helpful

1

P
Cassidy 160

@CMac450

Posted

@PatrickLee22 Thank you for the explanation! Both are great solutions.

0
P
ifaronti 580

@ifaronti

Posted

@PatrickLee22

I tried marking your suggestion as helping but I wasn't able to. your no cache method is golden. Thanks!!

0

@PatrickLee22

Posted

@ifaronti I appreciate the thought regardless. Glad I could help.

0

@asbhogal

Posted

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 the useState 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 the async/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 like transform: translateY(35px) for example. Another thing - your button shouldn't be wrapped in a div 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 helpful

0

P
Cassidy 160

@CMac450

Posted

@asbhogal Thank you Aman, your explanations and links are much appreciated!

1

@asbhogal

Posted

@CMac450 no problem - glad to help :)

0

Please log in to post a comment

Log in with GitHub
Discord logo

Join 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