考虑以下代码:
import React, { Component } from 'react';
class Counter extends Component {
state = { value: 5 };
increment = () => {
this.setState(prevState => ({
value: prevState.value + 1
}));
};
decrement = () => {
this.setState(prevState => ({
value: prevState.value - 1
}));
};
render() {
return (
<div>
{this.state.value}
<button onClick={this.increment}>+</button>
<button onClick={this.decrement}>-</button>
</div>
)
}
}
如何做到这一点,以便每当我单击减量按钮时,该值都将不小于0。该值的最小值将始终为零,而不是-1,-2,-3,-4 ... < / p>