-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathCalculator.tsx
33 lines (29 loc) · 947 Bytes
/
Calculator.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { useState } from "react";
interface CalculatorState {
total: number;
previousTotal: number;
}
export default function Calculator() {
const [calcState, setCalcState] = useState<CalculatorState>({ total: 0, previousTotal: 0 });
const [value, setValue] = useState(0);
const handleAddClick = () => {
setCalcState({ total: calcState.total + value, previousTotal: calcState.total});
};
return (
<div className="calculator">
<h2>Simple Calculator</h2>
<p>
<input
type="text"
id="value"
name="value"
onChange={(event) => { setValue(Number(event.target.value)); }}
value={value}
/>
</p>
<button className="btn btn-primary" type="button" onClick={handleAddClick}> Add </button>
<p className="card-text">Total: {calcState.total}</p>
<p className="card-text">Previous Total: {calcState.previousTotal}</p>
</div>
);
}