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

Calculator application using create-react-app with hook

Eric Nguyen•80
@EricNguyen1206
A solution to the Calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


This is my calculator application using create-react-app You guys can check my result at domain: https://calculator-react-app-psi.vercel.app/ Feel free to comment if you want to improve anything in this product. Thanks for visiting my repo!

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Katie•405
    @kem522
    Posted almost 4 years ago

    Heya! Good work! Very nice use of the useState hook.

    I have a couple of pieces of advice mostly surrounding the buttons on the calculator.

    You've used a div for those buttons but making them buttons is semantically preferable as it is more descriptive of what they are and it comes with accessibility benefits, the most important of which is that button elements are keyboard focusable. A user who can only use keyboard can use the tab key to move through interactive elements on the page, divs are not focusable by default but buttons are.

    Another benefit of using the button element is that you can leverage the value attribute in the click events and using e.target.value to pass in the value to the handleNumbers function, would look something like this:

    // the button would now just take handleNumbers as the onClick callback
    // I also added a value attribute equal to the value i'd expect this button to have on the calculator
    
    <button onClick={handleNumbers} className={`padButton maths ${theme==='2'?"text-color-2 btn-color-2":theme==='3'?"text-color-3 btn-color-3":""}`} id="seven" value="7">7</button>
    
    // in the handle numbers function you would use e.target.value 
     const handleNumbers = (e) => {
       const number = e.target.value; 
    
        if(currentVal.length >= 21) {
          alert('Digit Limit Met!');
        } else {
          if(expression === '0') {
            deleteBack();
          }
          display(number);
        }
        setCurrentVal(prev => prev + number);
      }
    
    

    In the example above, the only thing that changes between the button numbers is the text in the button and the value attribute, so you could use an array of numbers and a map to create the buttons instead of having to do them one by one:

     {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((num) => {
            return (
              <button
                onClick={handleNumbers}
                className={`padButton maths ${
                  theme === "2"
                    ? "text-color-2 btn-color-2"
                    : theme === "3"
                    ? "text-color-3 btn-color-3"
                    : ""
                }`}
                key={`button-${num}`}
                value={num}
              >
                {num}
              </button>
            );
          })}
    

    I was playing around with a very simple version of your app (it just logs the numbers in the console) in this codesandbox to make sure I was giving you the correct advice so feel free to take a look at it as it might be more helpful than trying to read the code here: https://codesandbox.io/s/vibrant-hill-bwbkv?file=/src/App.js

    Happy coding!

    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