I dont know how to return the result of plus and minus in my React calculator
Solution 1:
input text is a string so you are concat string and don't calculate sum you need to cast the string to a float for exemple your code will look like that
export default function App() {
const [number1, setNumber1] = useState(0);
const [number2, setNumber2] = useState(0);
const [sub, setSub] = useState(0);
function plus() {
setSub(parseFloat(number1) + parseFloat(number2));
}
function minus() {
setSub(parseFloat(number1) - parseFloat(number2));
}
return (
<div className="App">
Result = {sub} <br />
<input
placeholder="0"
value={Number(number1)}
onChange={(e) => setNumber1(e.target.value)}
/>
<input
placeholder="0"
value={Number(number2)}
onChange={(e) => setNumber2(e.target.value)}
/>
<button onClick={plus}>+</button>
<button onClick={minus}>-</button>
</div>
);
}