在每个问题组件上,我都试图清除超时。所以在 componentWillMount()
我将启动计时器,然后在 componentDidUpdate()
我将清除超时。计时器似乎不起作用。计时器到期后,我会将用户推送回主页。知道为什么使用 clearTimeout()
不起作用吗?
class Questions extends Component {
constructor(props){
super(props);
this.state ={
error: false,
position: null,
redirect: false
}
this.error = this.error.bind(this);
this.errorFalse = this.errorFalse.bind(this);
this.timer = this.timer.bind(this);
}
timer=()=>{
setTimeout(() => {
console.log('this ran')
this.setState({
redirect: true
})
}, 5000)
}
componentWillMount(){
this.setState({
error: false
})
this.timer();
}
componentDidUpdate(prevProps, prevState, snapshot, timer){
console.log('updated')
clearTimeout(this.timer);
}
原文由 jdip88 发布,翻译遵循 CC BY-SA 4.0 许可协议
反应挂钩