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

Tip calculator app, developed with TypeScript and React

react, typescript, vite
Harvey Yerik•370
@YerikAH
A solution to the Tip calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


Hello everyone, another challenge completed, this challenge was a bit difficult for me, if you think that the application has an error, I would really appreciate it if you could tell me about it.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • j0sephh123•140
    @j0sephh123
    Posted over 2 years ago

    Hello, adding some feedback

    • regarding typing in useState
    const [tipRender, setTipRender] = useState<tipPor[]>([]); // good
    const [billState, setBillState] = useState<string>(""); // no need for <string>
    const [erroBill, setErroBill] = useState<boolean>(false); // no need for <boolean>
    const [styleErrBill, setStyleErrBill] = useState<styleErr>(); 
    // here we could use null to handle when there is no error. It is a bit more verbose, but shows that error is null, i.e no error initially.
    const [styleErrBill, setStyleErrBill] = useState<styleErr | null>(null); 
    
    • Don't add types inside React components, either on top of the file or in another file depending on if it is used in multiple places or not.
    • const initialState - defined that also outside of the component
    • use CapitalCase for typescript type names
    • extract functions like trunc in different files like helpers or similar, they don't need to be defined inside the component. Keep functions like handleClick, which use components specific variables.
    • useMessage("Can't be zero"); - messages can be defined as variables and exported from another file
    • avoid deeply nested if's - in validationInput you have 3 levels deep ifs. Try to rewrite the function to avoid so much complexity.
    • function calcSimpleTip(): void if the function returns void, it doesn't need to be explicitly typed, it will be inferred from TS compiler.
    • in calcSimpleTip why billStateConvert and the other variables are defined with let ? Firstly, they can be const, also their type will be inferred. For example const billStateConvert = validationInput(billState, setErroBill, setErrMessBill); // will also have type number
    • Avoid casting variables to a type. It should be used only in rare cases, when there are no other options ( casting is ) let calctip = (billStateConvert * (porcentConvert / 100)) / peopleStateConvert; Here calctip doesnt need to be casted to number, because its type is inferred from the expression. Typescript is smart.
    • Again function resetStatusTip(): tipPor[] doesn't need to have explicit return type.
    • Regarding PartOneInput. Doesn't need to have type='text' since it it the default input prop. onBlur and onClick have e , but it is not used.

    Overall, good job. I would suggest the following:

    • split the application in smaller components
    • use useReducer function to handle the logic
    • refine Typescript knowledge a bit - it is endless, really, just a bit.
    Marked as helpful
  • Sebastian•510
    @sebix0nus
    Posted over 2 years ago

    Hello! 👋

    When I type in the number of people I should get the result right away, but unfortunately it only shows up when I press on the result panel. Maybe try to use .oninput event in a function on your input.

    Something like this in example: numberOfPeople.oninput = () => { }

    Cheers!

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