how to remove the warning “can't perform a react state update on unMounted Component”

前端 未结 1 1951
小蘑菇
小蘑菇 2021-01-21 23:24

I have an authentication system in which the user logins and if authenticated then redirect user to home page... it is working fine but the only problem is that it is giving me

相关标签:
1条回答
  • 2021-01-21 23:38

    if this a react app you are building, you call ajax request in componentDidMount lifecycle method. first set your private var _isMounted=false

    Then in componentDidMount:

    componentDidMount() {
        this._isMounted = true;
        .... your ajax request here
    }
    

    in your ajax success function:

    if (this._isMounted) {
        this.setState({ isLoading: false });
       }
    

    then in componentWillUnmount lifecycle method:

    componentWillUnmount(){
      this._isMounted= false
    }
    
    0 讨论(0)
提交回复
热议问题