[英]Using setInterval in React Component
I was reading the tutorial on the official react website. 我正在阅读官方反应网站上的教程。 In the example about life cycle methods, under the componentDidMount method, a timerID is set to the setInterval function.
在关于生命周期方法的示例中,在componentDidMount方法下,将timerID设置为setInterval函数。
My question is even though the timerID was initialized, it was never called throughout the application, how does the application work without explicitly calling timerID anywhere in the application. 我的问题是,尽管timerID已经初始化,但它从未在整个应用程序中调用过,如果没有在应用程序的任何地方显式调用timerID,应用程序如何工作。 Here is the code below.
这是下面的代码。
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
ReactDOM.render(
<Clock />,
document.getElementById('root')
);
this.timerID
is a numeric, non-zero value which identifies the timer created by the call to setInterval()
; this.timerID
是一个数字非零值,用于标识通过调用setInterval()
创建的计时器; this value can be passed to clearInterval
to clear the timer. 可以将此值传递给
clearInterval
以清除计时器。
So when calling the setInterval in componentDidMount like 所以在componentDidMount中调用setInterval之类的
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
you want to execute the tick()
function every 1 sec a
fter the component has mounted. 你想在组件安装的
every 1 sec a
执行tick()
函数。 Now when you navigate to another component and your current component has unmounted, if you do not clear the interval call to tick()
function, it will continue to be executed. 现在当您导航到另一个组件并且当前组件已卸载时,如果您没有清除对
tick()
函数的间隔调用,它将继续执行。
Hence in the componentWillUnmount
function you timer is cleared which is identified by the numeric value returned by setInterval
which is stored in this.timerID
因此,在
componentWillUnmount
函数中,清除了计时器,该计时器由setInterval
返回的数值标识,该值存储在this.timerID
componentWillUnmount() {
clearInterval(this.timerID);
}
so the complete code as provided in the React docs is 所以React文档中提供的完整代码是
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
ReactDOM.render(
<Clock />,
document.getElementById('root')
);
It's Simple. 这很简单。 As soon as it React executes componentDidMount() life cycle method, the timer starts running.
一旦React执行componentDidMount()生命周期方法,计时器就会开始运行。
this.timerID = setInterval(
() => this.tick(),
1000
);
The above timer will run until the component gets unmounted (according to your code). 上面的计时器将一直运行,直到卸载组件(根据您的代码)。 It's not a surprise that your code works that way.
您的代码以这种方式工作并不奇怪。
In this react document it's written that 在这份反应文件中写道
We will tear down the timer in the componentWillUnmount() lifecycle method
我们将在componentWillUnmount()生命周期方法中拆除计时器
So, this.timerID
will be used in componentWillUnmount()
lifecycle method to stop timer. 因此,
this.timerID
将在componentWillUnmount()
生命周期方法中用于停止计时器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.