You're not calling these function inside setCounter
, you're just passing arrow function definitions and not the calculated value of the counter, so then inside the if statement, you're comparing not the integer with integer, but a function definition with an integer.
Fixed code:
const [counter, setCounter] = React.useState(1);
function handleIncrement(counterArg) {
setCounter(counterArg + 1)
}
function handleDecrement(counterArg) {
if (counterArg > 1) {
setCounter(counterArg - 1)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…