I had this problem before, and solved it according to React official page isMounted is an Antipattern.
Set a property isMounted
flag to be true in componentDidMount
, and toggle it false in componentWillUnmount
. When you setState()
in your callbacks, check isMounted
first! It works for me.
state = {
isMounted: false
}
componentDidMount() {
this.setState({isMounted: true})
}
componentWillUnmount(){
this.setState({isMounted: false})
}
callback:
if (this.state.isMounted) {
this.setState({'time': remainTimeInfo});}