2 回答

TA贡献1773条经验 获得超3个赞
这是因为setState()是异步:
setState(newState,回调);
为了获得刚刚选择的难度,您必须像这样更改代码:
this.setState({
difficulty: e.target.value,
}, () => this.colorsArray(this.state.difficulty)
);

TA贡献1775条经验 获得超11个赞
问题在于您对setState的调用是否协调。以下是cleas的事情:
colorsArray(difficulty) {
const colors = colorsList(colorsListLength(difficulty));
const colorToGuess = randomColorToGuess(colors);
this.setState(() => ({
difficulty,
colorsList: colors,
gameTries: numberOfTries(this.state.difficulty),
colorToGuess
}));
}
handleChange(e) {
this.colorsArray(e.target.value);
}
您可以看到您的事件处理程序对颜色更新功能进行了一次调用。然后,计算出新的颜色并将状态设置在一个位置。
添加回答
举报