I have a function that starts a 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)
}
Now when my React component unmounts, I want to remove the timer from memory. So what I am currently doing is this:
componentWillUnmount() {
clearInterval(this.timer);
this.timer = undefined;
}
Is there a way to get rid of the timer altogether, without setting it to undefined?