试图遵循一个简单的时钟/倒计时教程阵营:为什么我的状态不确定?
constructor(props) {
super(props);
this.state = {
secondsRemaining: 10
};
}
componentDidMount(){
let interval = setInterval(this.timer, 1000);
this.setState({ secondsRemaining: this.state.secondsRemaining })
this.setState({ interval: interval });
};
componentWillUnmount() {
clearInterval(this.state.interval);
};
timer(){
this.setState({ secondsRemaining: this.state.secondsRemaining -1 });
};
很明显什么都没有,但是当我运行它,我得到一个错误的定时器功能说cannot read property secondsRemaining of undefined
这可能是什么愚蠢的我已经错过了,但我看不出它
跟着这个问题的答案:setInterval in a React app
'this.timer.bind(this)' –