didismusings.com

Creating a User-Friendly Tip Calculator Using React and JavaScript

Written on

Introduction to React

React is a straightforward JavaScript framework that simplifies the development of front-end applications. In this guide, we will explore the steps to build a tip calculator app utilizing both React and JavaScript.

Setting Up the Project

To initiate the React project, we can use Create React App. To do this, execute the following command:

npx create-react-app tip-calculator

This command will set up the foundational structure for your React application.

Building the Tip Calculator Application

To develop the tip calculator, we will start with the following code:

import { useState } from "react";

export default function App() {

const [subtotal, setSubtotal] = useState(0);

const [numDiners, setNumDiners] = useState(0);

const [tipPercentage, setTipPercentage] = useState(0);

const [result, setResult] = useState({});

const submit = (e) => {

e.preventDefault();

if (+subtotal <= 0 || +numDiners <= 0 || +tipPercentage <= 0) {

return;

}

const total = +subtotal * (1 + +tipPercentage);

setResult({ total, totalPerDiner: +total / +numDiners });

};

return (

<div className="App">

<form onSubmit={submit}>

<fieldset>

<label>Subtotal</label>

<input

value={subtotal}

onChange={(e) => setSubtotal(e.target.value)}

/>

</fieldset>

<fieldset>

<label>Number of People Sharing the Bill</label>

<input

value={numDiners}

onChange={(e) => setNumDiners(e.target.value)}

/>

</fieldset>

<fieldset>

<label>Tip Percentage</label>

<select

value={tipPercentage}

onChange={(e) => setTipPercentage(e.target.value)}

>

<option value="0">0%</option>

<option value="0.05">5%</option>

<option value="0.1">10%</option>

<option value="0.15">15%</option>

<option value="0.2">20%</option>

</select>

</fieldset>

<button type="submit">Calculate</button>

</form>

<p>Total: {result.total && result.total.toFixed(2)}</p>

<p>

Total per Diner:{" "}

{result.totalPerDiner && result.totalPerDiner.toFixed(2)}

</p>

</div>

);

}

In this code, we define states for the subtotal, number of diners, tip percentage, and the result.

  1. States:
    • subtotal represents the total amount before tips.
    • numDiners indicates how many people are sharing the bill.
    • tipPercentage stores the selected tip percentage (between 0 and 100).
    • result holds the calculated totals.
  2. Submit Function:
    • The submit function prevents the default form submission, checks for valid values, calculates the total and per diner amounts, and updates the result state accordingly.
  3. Rendering:
    • The return statement displays input fields, allowing users to enter their values. The form utilizes the onSubmit property to handle submissions, and the results are shown below.

Conclusion

With React and JavaScript, you can easily create a functional tip calculator. This project not only enhances your skills but also provides a practical tool for users.

This video demonstrates how to build a tip calculator app using React.js, providing a step-by-step tutorial to help beginners.

In this tutorial, learn to create a tip calculator in React JS with a comprehensive walkthrough suited for beginners in React development.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Exploring Brain Activity at the Moment of Death: Recent Insights

A look into new research revealing brain activity at death, shedding light on near-death experiences and consciousness.

Electrification vs. Digitalization: A Comparison of Megatrends

This article explores the contrasting success of electrification and digitalization megatrends, emphasizing project management's role in their outcomes.

Engagement: The Key to Achieving Success on Medium

Discover how engagement with others on Medium can significantly enhance your success and help you build a community.