Refreshing A Setinterval In React
This is based on the answer given here: I'm having trouble resetting a setInterval. As of now the following works. I have a prop called mediaList which contains an object array of
Solution 1:
window.setInterval
returns an id which identifies an Interval
timer. You can use it in conjunction with clearInterval
to cancel the interval.
this.interval = setInterval(...);
...
clearInterval(this.interval);
you can use componentWillReceiveProps
as kind of a generic method of checking to see if mediaList
has changed. for example:
componentWillReceiveProps(nextProps) {
if (nextProps.mediaList !== this.props.mediaList) {
clearInterval(this.interval);
}
}
Post a Comment for "Refreshing A Setinterval In React"