I want to change the value of currentIndex
every 5s but when it comes to this.setState
I receive Uncaught TypeError: this.setState is not a function
Here is my code:
constructor() {
super()
this.state = {
currentIndex: 0,
}
}
componentDidMount() {
this.changeIndex()
}
changeIndex() {
const { currentIndex } = this.state
setInterval(function () {
if (currentIndex < 2) {
this.setState({ currentIndex: currentIndex + 1 })
} else {
this.setState({ currentIndex: 0 })
}
}, 5000);
}