【问题标题】:React game of life (error when clicking too fast)反应生命游戏(点击太快时出错)
【发布时间】:2020-09-05 19:53:47
【问题描述】:

我正在使用 React 构建一个生活游戏。当我通过每秒单击 1-2 次来设置“活动”单元格时,它可以正常工作,但是当我尝试更快地单击时出现错误:

“类型错误:无法读取未定义的属性‘未定义’”。

指向函数表达式第四行的错误:

const onClick = (e) => {
    const indexies = e.target.id.split(",");
    const x = indexies[0];
    const y = indexies[1];
    setGrid([...grid], (grid[x][y] = !grid[x][y])); //error line
  };

【问题讨论】:

标签: javascript reactjs onclick dom-events


【解决方案1】:

使用函数更新网格状态可能会解决您的问题:

setGrid((prevGridState) => "return your new state here")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-03
    • 2021-04-09
    • 2016-06-24
    • 1970-01-01
    • 2013-08-19
    • 2013-11-30
    • 1970-01-01
    • 2014-04-30
    相关资源
    最近更新 更多