Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
ReactJS Calculator App (Styling)
Next article icon

ReactJS Calculator App (Building UI)

Last Updated : 27 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

We created our first app and deleted all those files we did not need, and created some of the files that we will need in the future. Now as we stand in our current situation we have a blank canvas before us where we will have to create our calculator app. 

We will be creating the project in multiple steps with a component approach and each file code is given below for you, so let’s start building the project.

  • Create a calculatorTitle.js file for showing the title of the calculator and paste the code given below for this file.

Javascript

//calculatorTitle.js File
  
import React from "react"; // Import React (Mandatory Step)
  
// Create Functional Component.
// Takes title as props.value.
const CalculatorTitle = (props) => {
    return (
    <div className="calculator-title">{props.value}</div>
    );
};
export default CalculatorTitle; // Export Calculator Title
                      
                       
  • Now create a file outputScreenRow.js for taking input and showing the output of the calculation, code of this file is given below.

Javascript

// outputScreenRow.js File
import React from "react"; // Import React (Mandatory Step)
  
// Functional Component.
// Used to show Question/Answer.
const OutputScreenRow = () => {
    return (
        <div className="screen-row">
            <input type="text" readOnly />
        </div>
    );
};
export default OutputScreenRow; // Export Output Screen Row
                      
                       
  • Create an outputScreen.js file and import the outputScreenRow.js file. The code of this file is given below.

Javascript

// outputScreen.js File
import React from "react"; // Import React (Mandatory Step).
import OutputScreenRow from "./outputScreenRow.js"; // Import Output Screen Row.
  
// Functional Component.
// Use to hold two Screen Rows.
const OutputScreen = () => {
    return (
        <div className="screen">
            <OutputScreenRow />
            <OutputScreenRow />
        </div>
    );
};
export default OutputScreen; // Export Output Screen.
                      
                       
  • Create a button.js file and paste the code given below.

Javascript

// button.js File
import React from "react"; // Import React (Mandatory Step)
  
// Create our Button component as a functional component.
const Button = (props) => {
    return (
    <input type="button" value={props.label} />
    );
};
export default Button; // Export our button component
                      
                       
  • Now create a calculator.js file and import calculatorTitle.js, outputScreen.js, and button.js files. The code for this program is below.

Javascript

// calculator.js File
// Imports.
import React from "react";
import CalculatorTitle from "./calculatorTitle.js";
import OutputScreen from "./outputScreen.js";
import Button from "./button.js";
  
class Calculator extends React.Component {
    render() {
        return (
            <div className="frame">
                <CalculatorTitle value="GeeksforGeeks Calculator" />
                <div class="mainCalc">
                    <OutputScreen />
                    <div className="button-row">
                        <Button label={"Clear"} />
                        <Button label={"Delete"} />
                        <Button label={"."} />
                        <Button label={"/"} />
                    </div>
                    <div className="button-row">
                        <Button label={"7"} />
                        <Button label={"8"} />
                        <Button label={"9"} />
                        <Button label={"*"} />
                    </div>
                    <div className="button-row">
                        <Button label={"4"} />
                        <Button label={"5"} />
                        <Button label={"6"} />
                        <Button label={"-"} />
                    </div>
                    <div className="button-row">
                        <Button label={"1"} />
                        <Button label={"2"} />
                        <Button label={"3"} />
                        <Button label={"+"} />
                    </div>
                    <div className="button-row">
                        <Button label={"0"} />
                        <Button label={"="} />
                    </div>
                </div>
            </div>
        );
    }
}
export default Calculator; // Export Calculator Component
                      
                       
  • Inside the index.js file import, the calculator.js file and the code for this file is given below.

Javascript

//index.js File
import React from "react";
import ReactDOM from "react-dom";
import Calculator from "./components/calculator.js";
  
// Render the Calculator to the Web page.
ReactDOM.render(<Calculator />, document.getElementById("root"));
                      
                       

Output: The output of this code will look like the below-given image.

So now we can finally see the output in our browser, but wait this is nothing like what we showed you in the introductory article! Yes, it is nowhere near to be the finished project, it is rather a barebone structure and all it needs is the CSS touch-ups that we will provide in one of the upcoming articles, but before that, we have to implement the working logic of this calculator so that at least it works before we transform this rigid design into some eye-catching masterpiece or at least a decent model. ReactJS | Calculator App ( Adding Functionality )



Next Article
ReactJS Calculator App (Styling)

P

PronabM
Improve
Article Tags :
  • Project
  • ReactJS
  • Web Technologies
  • ReactJS-Projects
  • Web Development Projects

Similar Reads

  • Build a Calculator using React Native
    React Native is a well-known technology for developing mobile apps that can run across many platforms. It enables the creation of native mobile apps for iOS and Android from a single codebase. React Native makes it simple to construct vibrant, engaging, and high-performing mobile apps. In this tutor
    6 min read
  • Build a Calculator with VueJS
    We'll learn how to build a simple calculator app using Vue.js. A calculator is a fundamental tool for performing mathematical calculations, and building one using Vue.js is a great way to understand the basics of Vue.js components, data binding, and event handling. Step-by-step guide to set up the p
    3 min read
  • Build a Simple Tip Calculator App with ReactJS
    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
    4 min read
  • ReactJS Calculator App (Styling)
    Now that we have added functionality to our Calculator app and successfully created a fully functional calculator application using React. But that does not look good despite being fully functional. This is because of the lack of CSS in the code. Let's add CSS to our app to make it look more attract
    3 min read
  • ReactJS Calculator App ( Structure )
    In our previous article, we have talked about a Calculator app we are going to develop and also have seen a glimpse of our final project. In this article, we will get our hands ready to start the development of our first application. We have told this earlier also that every application we will deve
    4 min read
  • BMI Calculator Using React
    In this article, we will create a BMI Calculator application using the ReactJS framework. A BMI calculator determines the relationship between a person's height and weight. It provides a numerical value that categorizes the individual as underweight, normal weight, overweight, or obese. Output Previ
    3 min read
  • Age Calculator Using React-JS
    In this article, we will create an Age Calculator using ReactJS and Bootstrap. This free age calculator computes age in terms of years, months, weeks, days, hours, minutes, and seconds, given a date of birth. Users can now input their birth year and calculate their current age with just a few clicks
    4 min read
  • Create a Calculator App Using Next JS
    Creating a Calculator app is one of the basic projects that clears the core concept of a technology. In this tutorial, we'll walk you through the process of building a calculator app using Next.js. Output Preview: Let us have a look at how the final output will look like. Prerequisites:NPM & Nod
    3 min read
  • Build a Loan Calculator using Next.js
    The Loan Calculator allows users to determine loan amount, interest rate, and tenure, calculate monthly mortgage payments, total loan payments over tenure, and total interest payable. In this article, we will walk you through the process of building a Loan Calculator using Next.js. Let's take a look
    4 min read
  • Calculator App Using TypeScript
    A calculator app is a perfect project for practising TypeScript along with HTML and CSS. This app will have basic functionalities like addition, subtraction, multiplication, and division. It provides a clean and interactive interface for the user while using TypeScript to handle logic safely and eff
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences