React Calculator App using useState (Complete Example)
๐ INTRODUCTION
Building a calculator is one of the best ways to understand:
- State management
- Event handling
- Dynamic UI updates
In this program, we will create a basic calculator app that can:
- Perform addition, subtraction, multiplication, division
- Display results dynamically
๐ This is a must-do mini project for React beginners.
๐ PROBLEM STATEMENT
๐ Develop a program to create a basic calculator (addition, subtraction, multiplication, division) using state in React.
๐ก CONCEPTS USED
- useState Hook
- Event Handling
- String Manipulation
- Dynamic Rendering
๐ง LOGIC EXPLANATION
- Store input expression in state
- Append values on button click
- Evaluate expression
- Display result
๐ป PROGRAM CODE
Open src/App.js:
import { useState } from "react";
function App() {
const [input, setInput] = useState("");
const handleClick = (value) => {
setInput(input + value);
};
const calculate = () => {
try {
setInput(eval(input).toString());
} catch {
setInput("Error");
}
};
const clear = () => {
setInput("");
};
return (
<div style={styles.container}>
<h1>Calculator</h1>
<input type="text" value={input} readOnly style={styles.display} />
<div style={styles.buttons}>
<button onClick={() => handleClick("1")}>1</button>
<button onClick={() => handleClick("2")}>2</button>
<button onClick={() => handleClick("3")}>3</button>
<button onClick={() => handleClick("+")}>+</button>
<button onClick={() => handleClick("4")}>4</button>
<button onClick={() => handleClick("5")}>5</button>
<button onClick={() => handleClick("6")}>6</button>
<button onClick={() => handleClick("-")}>-</button>
<button onClick={() => handleClick("7")}>7</button>
<button onClick={() => handleClick("8")}>8</button>
<button onClick={() => handleClick("9")}>9</button>
<button onClick={() => handleClick("*")}>*</button>
<button onClick={() => handleClick("0")}>0</button>
<button onClick={clear}>C</button>
<button onClick={calculate}>=</button>
<button onClick={() => handleClick("/")}>/</button>
</div>
</div>
);
}
const styles = {
container: {
textAlign: "center",
marginTop: "50px"
},
display: {
width: "200px",
height: "40px",
fontSize: "18px",
textAlign: "right",
marginBottom: "10px"
},
buttons: {
display: "grid",
gridTemplateColumns: "repeat(4, 50px)",
gap: "5px",
justifyContent: "center"
}
};
export default App;
๐ฅ๏ธ OUTPUT
๐ Example:
Input: 2 + 3
Output: 5
๐ STEP-BY-STEP EXPLANATION
useState("")โ stores input expressionhandleClick()โ appends valueseval()โ calculates resultsetInput()โ updates displayclear()โ resets input
โ ๏ธ IMPORTANT NOTE
๐ eval() is used for simplicity, but:
- Not recommended in production
- Use safer libraries for real apps
โก ADVANCED FEATURES
1. Add Decimal Support
<button onClick={() => handleClick(".")}>.</button>
2. Add Backspace
const backspace = () => {
setInput(input.slice(0, -1));
};
3. Improve UI with CSS
๐ Add colors, hover effects, grid styling
๐ฏ WHY THIS PROGRAM IS IMPORTANT
- Combines multiple React concepts
- Improves logical thinking
- Common interview project
- Helps build real apps
๐ REAL-WORLD USE CASE
- Calculator apps
- Financial tools
- Dashboard widgets
- Interactive UI components
โ VIVA QUESTIONS (IMPORTANT)
Q1. What is useState used for?
๐ To manage component state.
Q2. What is eval()?
๐ A function that evaluates JavaScript expressions.
Q3. Why avoid eval()?
๐ It can cause security issues.
Q4. What is event handling in React?
๐ Responding to user actions like clicks.
Q5. Can we build complex apps using this logic?
๐ Yes, this is the foundation.
๐ RELATED POSTS
๐ MCA 168 Full Stack Development Lab Programs List
๐ React Form Handling โ Program 16
๐ React Counter App โ Program 6
๐ React Toggle Visibility โ Program 8
๐ CONCLUSION
The calculator app is a powerful way to understand how React handles state and user interaction.
๐ Practice and enhance this project to build advanced applications.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
