【问题标题】:Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate超过最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况
【发布时间】:2018-03-11 16:10:58
【问题描述】:

我正在制作这个 Conway 的生命游戏 react 项目,它工作得很好,但是当我添加最后几个按钮来清除棋盘时,react 的一些其他功能给了我这个错误

Maximum update depth exceeded. This can happen when a component 
repeatedly calls setState inside componentWillUpdate or 
componentDidUpdate. React limits the number of nested updates to 
prevent infinite loops. 

从代码 sn-ps 来看,似乎 clear() 函数是这里的问题,但我认为我没有在 render() 中设置状态来触发无限循环。这是 clear 和 componentDidMount 的所有代码,我的应用程序中没有 componentWillUpdatecomponentDidUpdate

主类中的 clear() 和 Play 函数

编辑 1:它告诉我 play() 函数中的 setState 有问题,但是,我总是以这种方式实现 play 函数,它从一开始就一直在工作......

clear = ()=>{
    var g = Array(this.rows).fill().map(()=> Array(this.cols).fill(false));

    this.setState({
        generations:0,
        fullGrid: g
    })
}

.....

play = () => {

    let g1 = this.state.fullGrid;
    let g2 = arrayClone(this.state.fullGrid);

    for (let i = 0; i < this.rows; i++) {
        for (let j = 0; j < this.cols; j++) {
            let count = 0;

            if (i > 0)
                if (g1[i - 1][j]) count++;
            if (i > 0 && j > 0)
                if (g1[i - 1][j - 1]) count++;
            if (i > 0 && j < this.cols - 1)
                if (g1[i - 1][j + 1]) count++;
            if (j < this.cols - 1)
                if (g1[i][j + 1]) count++;
            if (j > 0)
                if (g1[i][j - 1]) count++;
            if (i < this.rows - 1)
                if (g1[i + 1][j]) count++;
            if (i < this.rows - 1 && j > 0)
                if (g1[i + 1][j - 1]) count++;
            if (i < this.rows - 1 && this.cols - 1)
                if (g1[i + 1][j + 1]) count++;
            if (g1[i][j] && (count < 2 || count > 3)) g2[i][j] = false;
            if (!g1[i][j] && count === 3) g2[i][j] = true;
        }
    }

    this.setState({
        fullGrid: g2,
        generations: this.state.generations + 1
    });

}


playButton = ()=>{
    clearInterval(this.intervalId);
    this.intervalId = setInterval(this.play, this.speed);
}

pauseButton = ()=>{
    clearInterval(this.intervalId);
}

slow = ()=>{
    this.speed = 1000;
    this.playButton();
}

fast = ()=>{
    this.speed = 100;
    this.playButton();
}

clear = ()=>{
    var g = Array(this.rows).fill().map(()=> Array(this.cols).fill(false))

    this.setState({
        generations:0,
        fullGrid: g
    })
}

按钮类

class Buttons extends React.Component{

handleSelect = (evt) =>{
    this.props.gridSize(evt);
}

render(){
    return (
        <div className="center">
            <ButtonToolbar>
                <button className='btn btn-info'  onClick={this.props.playButton}>
                    PLAY
                </button>
                <button className='btn btn-info'  onClick={this.props.pauseButton}>
                    PAUSE
                </button>
                <button className='btn btn-info'  onClick={this.props.clear}>
                    CLEAR
                </button>
                <button className='btn btn-info'  onClick={this.props.slow}>
                    SLOW
                </button>
                <button className='btn btn-info'  onClick={this.props.fast}>
                    FAST
                </button>
                <button className='btn btn-info'  onClick={this.props.seed}>
                    SEED
                </button>
                <DropdownButton
                    title="Grid Size"
                    id="size-menu"
                    onSelect={this.handleSelect}
                >
                    <MenuItem eventKey="1">20x10</MenuItem>
                    <MenuItem eventKey="2">50x30</MenuItem>
                    <MenuItem eventKey="3">70x50</MenuItem>
                </DropdownButton>
            </ButtonToolbar>
        </div>
    )
}

}

【问题讨论】:

  • 你能在按钮中为你的函数添加代码吗? seed,clear,fast,slow,pauseButton,playButton
  • 当然,添加:)
  • 不需要onClick={() =&gt; this.props.fast()) 吗?实际执行函数。
  • 哦,按钮中的onClick 方法现在都更改为该格式。我没有更新问题中的代码。但我已将其范围缩小到清除和种子方法。因为当我将这些注释掉时,代码可以正常工作。

标签: javascript reactjs render infinite-loop setstate


【解决方案1】:

您的onClick 处理程序被连续调用。将它们更改为返回您希望调用的函数的函数,这应该可以解决您的问题。

例子:

<button className='btn btn-info'  onClick={() => this.props.playButton}>
    PLAY
</button>

【讨论】:

  • 感谢您的回答,但不幸的是它仍然显示相同的错误。 :(
  • 您是否对所有功能进行了此更改?我只以playButton为例
  • 是的,我更改了所有这些并重新编译了应用程序,但它仍然显示相同的错误:(
  • 尝试一次删除一个函数调用并运行应用程序,看看我们是否能确定是哪个函数调用了它
  • 嘿,所以我注释掉了 clear 和 seed 方法,现在代码编译了,但只有暂停按钮或任何按钮不起作用,网格上也没有显示任何块.. .
猜你喜欢
  • 2020-10-22
  • 2020-12-11
  • 2023-04-09
  • 2019-08-12
  • 2021-04-29
  • 2019-07-25
  • 2023-03-26
  • 2018-09-13
  • 1970-01-01
相关资源
最近更新 更多