Cancel All Subscriptions and Asyncs in the componentWillUnmount Method, how?

前端 未结 3 1477
鱼传尺愫
鱼传尺愫 2020-12-09 14:58

I\'m getting an error message due to an async method issue. In my terminal I\'m seeing:

Warning: Can\'t call setState (or forceUpdate) on an unmounted compon         


        
3条回答
  •  囚心锁ツ
    2020-12-09 15:38

    From the React blog

    Just set a _isMounted property to true in componentDidMount and set it to false in componentWillUnmount, and use this variable to check your component’s status.

    It goes on to say that ideally, this would instead be fixed by using cancellable callbacks, although the first solution seems suitable here.

    What you definitely shouldn't do is use the isMounted() function, which may be deprecated.

提交回复
热议问题