Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 3 years ago

Random Advice Generator using React and SCSS

react, sass/scss
Tyler•330
@tylermaks
A solution to the Advice generator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


Hi all,

I really enjoyed this challenge! This was my first time attempt using async await with an API call, as well as useEffect. Any feedback on my async function is welcome!

    const getData = async () => {
        try{
            const res = await fetch('https://api.adviceslip.com/advice')
            const data = await res.json()
    
            setAdvice(data.slip.advice)
            setAdviceNum(data.slip.id)
        } catch (error){
            console.error(error)
        }   
    }

Thanks! -Tyler

Code
Couldn’t fetch repository

Please log in to post a comment

Log in with GitHub

Community feedback

  • Alan Ibarra•440
    @Ibarra11
    Posted almost 3 years ago

    One improvement you can make with your useEffect is to add an empty dependency list. As of right now, getData is getting called twice. When you click on the advice button, it calls the getData function, which then calls setState and React is going to queue a re-render. After React renders with the new state, it checks the useEffect and since you don't have any dependencies, React is going to run the useEffect after every render, which causes getData to get called again. In this case, you only want the useEffect to run on the initial render to get the first advice and from that point forward the action of getting new advice data should come from clicking the button.

    useEffect(() =>{
     getData()
    }, [])
    
    Marked as helpful
  • Elaine•11,360
    @elaineleung
    Posted almost 3 years ago

    Hi Tyler, the app looks good! As @shivaprakash-sudo said, on Firefox the advice only either loads on the first load and then doesn't load, or it would need a few seconds for the next load despite clicking on the button. I solved this problem by adding a cache object in fetch:

    fetch("https://api.adviceslip.com/advice", { cache: 'no-cache' })
    

    If you have Firefox, try it out and see.

    Marked as helpful
  • Shiva•670
    @shivaprakash-sudo
    Posted almost 3 years ago

    Hello Tyler,

    The code looks fine to me.

    Side note: I don't know why, but the app doesn't seem to work in my Firefox, it works on other browsers, but even in them, I have to wait for 2 or 3 seconds to click the button for new advice, since clicking the button immediately doesn't seem to work. I tested the code too on my machine and the same thing happens, for some reason. Has this ever happened to you?

    Marked as helpful
  • Bryan Sanchez•590
    @0-BSCode
    Posted almost 3 years ago

    Awesome work, Tyler! Your async await code looks good. For future projects, I would suggest getting into organizing your React project's file structure. You can read more about it here. Although for simple projects like this one it may not be needed, I think it's a good idea to get into the habit of organizing your files early on so that when you eventually get into large projects it's easier to structure them.

    Keep up the good work! Cheers🎉

    Marked as helpful

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
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub