Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted 10 months ago

Tip calculator app - React

react
sherimin•380
@sherimin
A solution to the Tip calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

In this application, I refreshed my knowledge of React hooks to create this tip calculator. The tip amount is calculated simultaneously as the user inputs the total bill, tip percentage, and number of people, providing instant feedback. I also enhanced my understanding of adding active or error stylings to elements, for better user experience through intuitive form validation.

I think for next time, I would refactor the code with a useReducer hook for cleaner code structure when dealing with multiple state variables

What specific areas of your project would you like help with?

None that I can think of, but any input or advice is welcome!

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • P
    kephalosk•400
    @kephalosk
    Posted 3 months ago

    Nice solution. I like the counters that you used in the input fields. I also had a look at your code. I noticed that you coded everything in one god component. That's ok in the beginning to get familiar with React. As you make more progress you could think of breaking your code down into atom components with single responsibilities. In your given example: 3 separate custom hooks for useTipCalculation, useInputHandling and useResetInput. From your html-template you could extract components like BillInput, TipInput, PeopleInput and Result and use App as a container for these. Modular structures like this make your code easier to read, to understand and to maintain. A second hint I can give you is to use useMemo, useCallback and React.memo to optimize the performance of React. E.g.you have the function tipCalculation(). At this moment this functions is redefined with every new render which costs valuable performance and makes this function unstable. I guess that could be a reason why you don't include tipCalculation in the dependency Array of your useEffect(), because with unstable dependent functions useEffect gets triggered with every render. with useCallback() you can prevent React from doing that if no dependent value has changed:

    const tipCalculation = useCallback(() => {
        let tipDecimal = 0
        if (Number(numOfPeople) > 0) {
          if (customTip) {
            tipDecimal = customTip / 100;
          } else {
            tipDecimal = tipPercentage / 100;
          }
          
          const totalTip = billAmount * tipDecimal;
          const totalAmountWithTip = billAmount + totalTip;
    
          setTotalPerPerson((totalAmountWithTip / Number(numOfPeople)).toFixed(2));
          setTipPerPerson((totalTip / Number(numOfPeople)).toFixed(2));
          setError(false);
        }
      },[billAmount, numOfPeople, tipPercentage, customTip]);
    

    Now you have a stable function that is only recalculated if billAmount, numOfPeople, tipPercentage or customTip change. All over a compact solution. Keep up the good work :)

    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

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