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.
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.
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;
/* 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; }
GitHub is a powerful tool used by teams and developers around the globe. This guide is…
It's like having a super-smart buddy that is always there to help you write stories,…
The UK is known for its rich history, diverse culture, and most of all its…
Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…
The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…
Google Gemini AI is among the top talked about developments. What exactly is it? What…