Search code examples
react-nativesetstate

Can't call setState (or forceUpdate) on an unmounted component on set state


how we can use setState in componentDidMount() or how we can change state value?. While using below code getting error

Can't call setState (or forceUpdate) on an unmounted component

componentDidMount(){
  this.interval = setInterval(() => {
    if(this.state.rmSec==0){
      this.setState({
        rmSec:59,
        rmMin:this.state.rmMin-this.state.minus,
      })
    }else{
      this.setState({
        rmSec:this.state.rmSec-this.state.minus
      })
    }

    if(this.state.rmMin==0){
      this.setState({
        rmSec:0,
        rmMin:0,
      })

    }


  }, 1000);
}

Solution

  • Issue solved by clearing interval on unmount

    componentWillUnmount () {
        this.interval && clearInterval(this.interval);
        this.interval = false;
    }