I am working on a react application. I have a button which call a function inside which I change a state of my app. When a button is clicked, I want to keep on changing the state till the button is again clicked (to stop). Currently I am doing it like this
state = {
        startX:0
    }
changeX = () => {
        //console.log("changex")
        this.setState({startX: this.state.startX + (1/25)} , ()=>{
            console.log(this.state.startX)
            this.changeX()
        })
    }
<button onClick={this.changeX.bind(this)}>Change</button>
I get an error saying
Maximum update depth exceeded. This can happen when a component 
repeatedly calls setState inside componentWillUpdate or 
componentDidUpdate. React limits the number of nested updates 
to prevent infinite loops.
 
    