我正在通过React.js构建生命游戏,我陷入了一种不舒服的境地:我设置的每个事件都onClick={ event }需要2次点击才能执行.
让我来描述一下: 正如你在下面的代码中看到的,我有2个按钮(一个按钮是将电路板的尺寸改为10×10,另一个是改变间隔的速度).
一切都很好,除了当我点击这两个按钮时,我需要双击才能执行.在第一次点击时,使用Chrome中的React Developer Tool,我可以看到状态包括width, height, speed已更改,但状态board仍保持不变.仅在第二次单击后,board状态才会更改.
任何人都可以解释原因,并告诉我如何解决?谢谢
这是我的代码的一部分
var GameBoard = React.createClass({
getInitialState: function() {
return {
width: 10,
height: 10,
board: [],
speed: 1000,
};
},
// clear the board to the initial state
clear: function(width, height) {
this.setState({
width: width,
height: height,
});
this.setSize();
clearInterval(this.game);
},
// set the size of the board
setSize: function() {
var board = [];
for (var i = 0; i …Run Code Online (Sandbox Code Playgroud)