Open In App

Build a Simple Tip Calculator App with ReactJS

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Creating a simple tip calculator app using ReactJS can be good for practicing React state management and components. In this tutorial, we’ll create a basic tip calculator app using React.js. The app will allow users to input the bill amount, select a tip percentage and let split the bill, providing them with the calculated tip amount and total bill.

 Prerequisites:

Approach:

Create a simple React application with the help of the below installation procedure that will calculate the Tip value based on the Total Bill value. This simple bill-splitting calculator will allow the user to enter the total bill amount and choose a tip percentage. The user can also adjust the number of splits. The split total is automatically calculated and displayed. The code uses React hooks (useState and useEffect) to manage the state and perform calculations based on user input.

Steps to create React Application

Step 1: Create a React application using the following command:

npx create-react-app tip-calculator

Step 2: After creating your project folder i.e. react-app, move to it using the following command:

cd tip-calculator

Project Structure

Untitled.png

project structure

Example: Write the following code in App.js and App.css file

CSS
/* App.css */

body {
    background: #f2f2f2;
    color: #333;
    padding: 20px;
}

label {
    display: block;
    text-transform: uppercase;
    font-size: 0.8rem;
    line-height: 0.5rem;
    padding-left: 1px;
    color: rgba(0, 0, 0, 0.7);
}

input[type="text"] {
    background: transparent;
    border: 0;
    margin-top: 10px;
    margin-bottom: 10px;
    color: #555;
    font-size: 1.4rem;
}

.summary {
    background: #ffcc00;
    padding: 10px;
    border-radius: 10px;
    display: flex;
    justify-content: space-between;
    font-size: 1.7rem;
}

.summary label {
    line-height: 0.8rem;
}

.split-control {
    display: flex;
    align-items: center;
    justify-items: center;
}

.split-control span {
    display: block;
    padding: 0 3px;
}

.split button {
    border-radius: 99999999px;
    border: 0;
    width: 20px;
    height: 20px;
    background: #4caf50;
    color: #fff;
    cursor: pointer;
}

.result {
    text-align: right;
}

main {
    width: 200px;
    margin: 20px auto;
}
JavaScript

Steps to Run Application:

Step 1: Run the application using the following command from the root directory of the project:

npm start

Step 2: Type the following link in your browser

http://localhost:3000/

Output:



Next Article

Similar Reads

three90RightbarBannerImg