React JS

How to Create a Functional Calculator in React

Introduction to React Calculator

Creating a functional calculator in React is an excellent way to enhance your skills in this popular JavaScript library. This guide will walk you through the essential steps to build a simple yet effective calculator application.

Setting Up the Project

To start, you need to set up your React project. You can easily do this using Create React App with Vite. Open your terminal and run the

$ npm create vite@latest

Select as React from the following list and and install it.

App.jsx

import React, { useState } from "react";
import "./App.css";

const App = () => {
  const [display, setDisplay] = useState("");

  const handleClick = (value) => {
    if (value === "=") {
      try {
        setDisplay(eval(display).toString()); // Be cautious using eval in production
      } catch {
        setDisplay("Error");
      }
    } else if (value === "C") {
      setDisplay("");
    } else {
      setDisplay(display + value);
    }
  };

  const buttons = [
    "C", // "C" will be styled to span the entire width
    "7", "8", "9", "/",
    "4", "5", "6", "*",
    "1", "2", "3", "-",
    "0", ".", "=", "+"
  ];

  return (
    <div className="calculator">
      <div className="display">{display || "0"}</div>
      <div className="buttons">
        {buttons.map((btn, index) => (
          <button
            key={index}
            onClick={() => handleClick(btn)}
            className={btn === "C" ? "clear" : btn === "=" ? "equals" : ["+", "-", "*", "/"].includes(btn) ? "operator" : ""}
          >
            {btn}
          </button>
        ))}
      </div>
    </div>
  );
};

export default App;

App.css

/* General styles */body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(to top, #6a11cb, #2575fc);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}

.calculator {
  background: #1e1e2f;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.7);
  width: 320px;
  display: flex;
  flex-direction: column;
}

.display {
  background: #282c34;
  color: #eaeaea;
  padding: 15px;
  font-size: 2rem;
  text-align: right;
  border-bottom: 2px solid #4a4e69;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  overflow: hidden;
}

.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding: 10px;
}

button {
  background: #4a4e69;
  color: #eaeaea;
  border: none;
  border-radius: 8px;
  font-size: 1.5rem;
  padding: 15px;
  cursor: pointer;
  transition: background 0.3s, transform 0.2s;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  -webkit-transition: background 0.3s, transform 0.2s;
  -moz-transition: background 0.3s, transform 0.2s;
  -ms-transition: background 0.3s, transform 0.2s;
  -o-transition: background 0.3s, transform 0.2s;
}

button:hover {
  background: #5c5f77;
  transform: translateY(-2px);
}

button:active {
  transform: translateY(2px);
}

button.operator {
  background: #9c27b0;
  color: white;
}

button.operator:hover {
  background: #8e24aa;
}

button.operator:active {
  background: #7b1fa2;
}

button.clear {
  background: #e53935;
  color: white;
  grid-column: span 4; /* Full width */}

button.clear:hover {
  background: #c62828;
}

button.clear:active {
  background: #b71c1c;
}

button.equals {
  background: #4caf50;
  color: white;
}

button.equals:hover {
  background: #388e3c;
}

button.equals:active {
  background: #2e7d32;
}

I attached the Video Below.do the work while watching the Video

 

 

 

 

 

admin

Recent Posts

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

2 weeks ago

Build Your First Responsive Login Form Using HTML and CSS FlexBox

Creating a responsive login form is a crucial skill for any web developer. In this…

2 weeks ago

Build Crud API with Laravel 12

In this tutorial will teach  Laravel 12 CRUD API  by step. Laravel  10 CRUD Application …

3 weeks ago

laravel 12 image upload tutorial

In this lesson we talk about laravel 12 image uploading and display the image step…

4 weeks ago

Laravel 12 CRUD Application

In this tutorial will teach Laravel 12 CRUD Application step by step. Laravel  12 CRUD…

1 month ago

Conditional Statements in Python

Conditional statements in Python allow us to control the flow of execution based on conditions.…

2 months ago