Unable to stop setInterval in react using componentWillUnmount() when the timer hits at 7 class component
export default class App extends React.Component{
constructor(props){
super(props);
this.state={
counter:0
}
}
componentDidMount(){
this.timerID = setInterval(
() => this.timer(),
1000
);
}
componentWillUnmount(){
if(this.state.counter===7){
clearInterval(this.timerID);
}
}
timer = () => {
this.setState({
counter: this.state.counter+1
})
}