Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 2 years ago

Advice generator app using React and Advice Slip API

accessibility
andyjv1•320
@andyjv1
A solution to the Advice generator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


I had a few issues with the API and the button while building this project. It seems to be fixed now. Any Feedback is welcomed.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Andreas Remdt•950
    @andreasremdt
    Posted about 2 years ago

    Hey @andyjv1,

    Nice job finishing this challenge! Since @grace-snow already mentioned some things, I'll purely focus on the React/JavaScript part:

    • You have 2 fetchAdvice functions, one in App.jsx and the other in Dicecontainer.jsx. This is duplicated code that you should try and avoid. Ideally, you keep the fetch function inside App.jsx and trigger it with a prop like onClick, which you pass into Dicecontainer. You can then remove the fetchAdvice inside this component and connect the prop with the button's click event.
    • The logic inside fetchAdvice can be improved. You don't need a timeout to disable/enable the button, this should be depending on the state of the promise. Consider something like this:
      const fetchAdviceClicked = async () => {
        setDisabled(true);
        const API_LINK = "https://api.adviceslip.com/advice";
        const response = await fetch(API_LINK);
        const advice = await response.json();
        props.setText(advice.slip)
        setDisabled(false);
      };
    

    Right before the request is made, you disable the button. Once the request went through (and assuming it was successful), you enable it again.

    • You don't have any error handling in your code. As an example, what happens if the request can't be made because the user is offline? In the DevTools, you'd see an error, but the common user doesn't have DevTools open :D Try thinking about ways to show an error, even if it's not part of the design. As an example, you could use try/catch:
      const fetchAdviceClicked = async () => {
        try {
            setDisabled(true);
            const API_LINK = "https://api.adviceslip.com/advice";
            const response = await fetch(API_LINK);
            const advice = await response.json();
            props.setText(advice.slip)
            setDisabled(false);
        } catch (ex) {
            setError("Something went wrong...");
        }
      };
    
    • Even though you disabled the button while disabled is truthy, it still has a hover state, which is confusing. Consider adding hover and focus states only when the button is enabled, otherwise, just gray it out. One way to solve this would be by using CSS pseudo classes: button:not(:disabled):hover { ... your code here }
    • Your initial state in App.jsx is an empty array, but the advice slip is an object with properties, such as id. Changing a variable type rarely a good thing, so consider changing the initial state to {} or null.

    Let me know if you have any questions, I hope my comments are helpful. Keep up the good work and happy coding!

    Marked as helpful
  • Grace•32,130
    @grace-snow
    Posted about 2 years ago

    Hi

    You need to change the HTML in this. It's not accessible / appropriate at the moment

    1. Advice # is the heading
    2. The advice text itself is a paragraph or could use a blockquote
    3. Use the picture element for responsive images not 2 img elements
    4. The line is decorative so alt must be blank
    5. The button must be properly labelled with what clicking it will do. I would do this with text inside the button and leave the button img alt blank
    6. Because some assistive tech users will not know that advice has changed after clicking the button I recommend wrapping everything in an aria-live element (eg add the attribute to smallcontainer)

    Some of the styling looks very strange on mobile with the advice box positioned half off the screen. I wouldn't expect this design to need a media query at all tbh. I will add some images to discord of what I see. It's probably the width 80% on larger screens and the margin top on smaller screens both causing issues

    However you definitely should be working mobile first even if this does need a media query.

    And letter spacing must never ever be in px! i recommend em

    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 1st-party linked stylesheets, and styles within <style> tags.

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.

Oops! 😬

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

Log in with GitHub

Frontend Mentor for Teams

Frontend Mentor for Teams helps companies and schools onboard and train developers through project-based learning. Our industry-standard projects give developers hands-on experience tackling real coding problems, helping them master their craft.

If you work in a company or are a student in a coding school, feel free to share Frontend Mentor for Teams with your manager or instructor, as they may use it to help with your coding education.

Learn more

Oops! 😬

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

Log in with GitHub