Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this article, we are going to make a calculator in React. It will be similar to an Android calculator application where you put your values which remain on the screen and show the result straight away as you put values along with some operation. Its logic should be easy, as we do in most of our programming languages. The only difference is that we will make a UI like any other calculator has.
So basically this is going to be a beginner-friendly project, so let’s make this project step-by-step.
Now we have added buttons for the operator and some symbols like dot(.) and equal(=) along with delete. After that, we need to put all 9 digits, for that, we have added a function named createDigits, in this, we have used a loop to add 1-9 digit buttons and added them to an application using the push() method.
function App() {
const createDigits = () => {
const digits = [];
for (let i = 1; i < 10; i++) {
digits.push(<button key={i}>{i}</button>);
}
return digits;
}
return (
<div className="App">
<div className="calculator">
<div className="display">
<span>0</span> {0}
</div>
<div className="operators">
<button>/</button>
<button>x</button>
<button>-</button>
<button>+</button>
<button>DEL</button>
</div>
<div className="digits">
<button >0</button>
<button >.</button>
<button >=</button>
</div>
</div>
</div>
);
}
export default App;
:root {
--primary: #d81e5b;
--secondary: #8c38ff;
--dark: #131a26;
--light: #EEE;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Fira Sans', sans-serif;
}
body {
background-color: var(--light);
}
.App {
display: flex;
min-height: 100vh;
align-items: center;
justify-content: center;
}
.calculator {
width: 100%;
max-width: 400px;
background-color: #FFF;
border-radius: 16px;
overflow: hidden;
box-shadow: 0px 2px 64px rgba(0, 0, 0, 0.2);
}
.display {
padding: 16px;
text-align: right;
background-color: var(--dark);
color: var(--light);
font-size: 24px;
font-weight: 300;
}
.display span {
font-size: 14px;
color: #888;
}
.operators {
display: flex;
}
button {
appearance: none;
outline: none;
border: none;
color: #FFF;
padding: 16px;
font-size: 20px;
cursor: pointer;
transition: 0.4s;
}
button:hover {
opacity: 0.9;
}
.operators button {
flex: 1 1 0%;
background-color: var(--primary);
font-weight: 700;
}
.digits {
display: flex;
flex-wrap: wrap;
}
.digits button {
flex: 1 1 33.333%;
max-width: 33.333%;
background-color: var(--dark);
}
import { useState } from 'react';
function App() {
const [calc, setCalc] = useState("");
const [result, setResult] = useState("");
const ops = ['/', '*', '-', '+', '.'];
}
After that we set the value of the result, if their ops do not contain any number then we simply put in the result. Now we have updated our dynamic result with the state named result, and also we have applied the click event on all of the operators and symbols where we are calling the updateCalc function to print the entered button. Then we added a function on the “=” button to calculate the equation. and Also another click event function on the delete button.
const createDigits = () => {
const digits = [];
for (let i = 1; i < 10; i++) {
digits.push(<button onClick={() => updateCalc(i.toString())} key={i}>{i}</button>);
}
return digits;
}
const updateCalc = (value) => {
if (
ops.includes(value) && calc === '' ||
ops.includes(value) && ops.includes(calc.slice(-1))
) {
return;
}
setCalc(calc + value);
if (!ops.includes(value)) {
setResult(eval(calc + value).toString());
}
}
return (
<div className="App">
<div className="calculator">
<div className="display">
<span>{result ? '(' + result + ')' : ''}</span> {calc || 0}
</div>
<div className="operators">
<button onClick={() => updateCalc('/')}>/</button>
<button onClick={() => updateCalc('*')}>x</button>
<button onClick={() => updateCalc('-')}>-</button>
<button onClick={() => updateCalc('+')}>+</button>
<button onClick={deleteLast}>DEL</button>
</div>
<div className="digits">
{createDigits()}
<button onClick={() => updateCalc('0')}>0</button>
<button onClick={() => updateCalc('.')}>.</button>
<button onClick={calculate}>=</button>
</div>
</div>
</div>
);
After that, in the deleteLast() function, if calc is blank then obviously we have nothing to do. We just delete the last value using the slice method, here -1 represents the deletion of one digit or ops from the end. then again, we will update the calc value to get a reflection on the screen. and we will update the result value since we need the dynamic result.
const calculate = () => {
setCalc(eval(calc).toString());
}
const deleteLast = () => {
if (calc == '') {
}
const value = calc.slice(0, -1);
setCalc(value);
setResult(eval(value).toString());
}
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
import { useState } from 'react';
function App() {
const [calc, setCalc] = useState("");
const [result, setResult] = useState("");
const ops = ['/', '*', '-', '+', '.'];
const createDigits = () => {
const digits = [];
for (let i = 1; i < 10; i++) {
digits.push(<button onClick={() => updateCalc(i.toString())} key={i}>{i}</button>);
}
return digits;
}
const updateCalc = (value) => {
if (
ops.includes(value) && calc === '' ||
ops.includes(value) && ops.includes(calc.slice(-1))
) {
return;
}
setCalc(calc + value);
if (!ops.includes(value)) {
setResult(eval(calc + value).toString());
}
}
const calculate = () => {
setCalc(eval(calc).toString());
}
const deleteLast = () => {
if (calc == '') {
}
const value = calc.slice(0, -1);
setCalc(value);
setResult(eval(value).toString());
}
return (
<div className="App">
<div className="calculator">
<div className="display">
<span>{result ? '(' + result + ')' : ''}</span> {calc || 0}
</div>
<div className="operators">
<button onClick={() => updateCalc('/')}>/</button>
<button onClick={() => updateCalc('*')}>x</button>
<button onClick={() => updateCalc('-')}>-</button>
<button onClick={() => updateCalc('+')}>+</button>
<button onClick={deleteLast}>DEL</button>
</div>
<div className="digits">
{createDigits()}
<button onClick={() => updateCalc('0')}>0</button>
<button onClick={() => updateCalc('.')}>.</button>
<button onClick={calculate}>=</button>
</div>
</div>
</div>
);
}
export default App;
:root {
--primary: #d81e5b;
--secondary: #8c38ff;
--dark: #131a26;
--light: #EEE;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Fira Sans', sans-serif;
}
body {
background-color: var(--light);
}
.App {
display: flex;
min-height: 100vh;
align-items: center;
justify-content: center;
}
.calculator {
width: 100%;
max-width: 400px;
background-color: #FFF;
border-radius: 16px;
overflow: hidden;
box-shadow: 0px 2px 64px rgba(0, 0, 0, 0.2);
}
.display {
padding: 16px;
text-align: right;
background-color: var(--dark);
color: var(--light);
font-size: 24px;
font-weight: 300;
}
.display span {
font-size: 14px;
color: #888;
}
.operators {
display: flex;
}
button {
appearance: none;
outline: none;
border: none;
color: #FFF;
padding: 16px;
font-size: 20px;
cursor: pointer;
transition: 0.4s;
}
button:hover {
opacity: 0.9;
}
.operators button {
flex: 1 1 0%;
background-color: var(--primary);
font-weight: 700;
}
.digits {
display: flex;
flex-wrap: wrap;
}
.digits button {
flex: 1 1 33.333%;
max-width: 33.333%;
background-color: var(--dark);
}