gpt4 book ai didi

javascript - ES6 - 警告 : setState(…): Cannot update during an existing state transition

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:41:24 30 4
gpt4 key购买 nike

我正在重写一些旧的 ReactJS 代码,但在修复这个错误时遇到了困难(错误在控制台中重复了大约 1700 次,DOM 根本没有呈现):

Warning: setState(...): Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.

我是一个组件,它将它的状态传递给一个应该呈现一些控件的组件。基于点击的控件,状态应该改变,新的控件应该呈现。

所以这是我的容器组件:

class TeaTimer extends Component {
constructor(props) {
super(props);
this.state = {
count: 120,
countdownStatus: 'started'
}
}

componentDidUpdate(prevProps, prevState) {
if (this.state.countdownStatus !== prevState.countdownStatus) {
switch (this.state.countdownStatus) {
case 'started':
this.startTimer();
break;
case 'stopped':
this.setState({count:0});
}
}
}

componentWillUnmount() {
clearInterval(this.timer);
delete this.timer;
}

startTimer() {
this.timer = setInterval(() => {
let newCount = this.state.count -1;
this.setState({
count: newCount >= 0 ? newCount : 0
});
if(newCount === 0) {
this.setState({countdownStatus: 'stopped'});
}
}, 1000)
}

handleStatusChange(newStatus) {
this.setState({ countdownStatus: newStatus });
}

render() {
let {count, countdownStatus} = this.state;
let renderStartStop = () => {
if (countdownStatus !== 'stopped') {
return <StartStop countdownStatus={countdownStatus} onStatusChange={this.handleStatusChange()}/>
} else {
return <div>This will be the slider form</div>
}
};
return(
<div className={styles.container}>
<p>This is the TeaTimer component</p>
<Clock totalSeconds={count}/>
{renderStartStop()}
</div>
)
}
}

这是我的控件组件:

class StartStop extends Component {
constructor(props) {
super(props);
}

onStatusChange(newStatus) {
return() => {
this.props.onStatusChange(newStatus);
}
}

render() {
let {countdownStatus} = this.props;

let renderStartStopButton = () => {
if(countdownStatus === 'started') {
return <button onClick={()=> this.onStatusChange('stopped')}>Reset</button>;
} else {
return <button onClick={()=> this.onStatusChange('started')}>Start</button>
}
};

return(
<div className={styles.tt.Controls}>
{renderStartStopButton()}
</div>
)
}
}

StartStop.propTypes = {
countdownStatus: React.PropTypes.string.isRequired,
onStatusChange: React.PropTypes.func.isRequired
};

对于文字墙,我很抱歉,但我真的无法弄清楚错误的来源 - 因此不知道我可以省略代码的哪一部分。

我已经尝试实现在 seemingly related question 中找到的解决方案,但也无法正常工作。

最佳答案

我认为你在这一行中有错字:

return <StartStop countdownStatus={countdownStatus} onStatusChange={this.handleStatusChange()}/> 

应该是:

return <StartStop countdownStatus={countdownStatus} onStatusChange={() => this.handleStatusChange}/>

您似乎在调用方法 handleStatusChange 而不是将其作为回调传递。

关于javascript - ES6 - 警告 : setState(…): Cannot update during an existing state transition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41426904/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com