I am incrementing a counter by setState() of React using a setInterval method. But for every intervals the application is updating multiple times.
import React, { Component } from 'react';
export default class Arrival extends Component {
state = {
count: 0,
limitTo: 6
}
render() {
try {
setInterval(() => {
this.setState({
limitTo: this.state.limitTo + 6
})
console.log(this.state)
}, 5000);
} catch(err) {
console.log(err)
}
return()
}
}
In the first 5 seconds, the state is getting updated once.
In the next 5 seconds, the state is getting updated 2 times.
In the next 5 seconds, the state is getting updated 4 times. and so on...
I want the state to only get updated once every 5 seconds.
setInterval()tocomponentDidMount()