[英]How to set max and min value for increment and decrement methods inside React
所以问题是我正在尝试创建一个反馈应用程序,用户可以在其中对服务进行 1 到 10 的评分。这是一个简单计数器应用程序的升级,但我想在我的应用程序中添加一些新的东西,问题是我不知道如何限制最大值和最小值的范围。 这是我的代码:
state = {
headline: 'Please rate our service from 1 to 10',
scale: '1: Extremly bad - 10: Extremely good',
points: [
{ id: 1, question: 'What do you think about our food?', value: 1 },
{ id: 2, question: 'What do you think about our staff?', value: 1 },
{ id: 3, question: 'What do you think about our menu?', value: 1 },
{ id: 4, question: 'What do you think about our cleanliness?', value: 1 }
]
};
handleIncrement = point => {
const points = [...this.state.points];
const index = points.indexOf(point);
points[index] = { ...point };
points[index].value++;
this.setState({ points });
};
感谢@palaASн,我已经解决了这个问题。 这是解决方案:
handleIncrement = point => {
const points = [...this.state.points];
const index = points.indexOf(point);
points[index] = { ...point };
if (points[index].value < 10) {
points[index].value++;
}
this.setState({ points });
};
handleDecrement = point => {
const points = [...this.state.points];
const index = points.indexOf(point);
points[index] = { ...point };
if (points[index].value > 1) {
points[index].value--;
}
this.setState({ points });
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.