React componentWillUnmount 清除异步state操作

React componentWillUnmount 清除异步state操作

Arthur
2023-01-31 / 0 评论 / 103 阅读 / 正在检测是否收录...

React componentWillUnmount 清除异步state操作

Can’t perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

原因:

通常是 react 组件已经从 DOM 中移除,但是我们在组件中做的一些异步操作还未结束,如:接口调用等,当其完成时,执行setState操作,而此时我们已经将改组件dom移除,从而导致上述问题。

解决办法:

    componentWillUnmount() {
        this.setState = (state, callback) => { return; }
    }
0
如果你觉得文章还不错,可以请我喝杯咖啡啊哈哈哈
wechat alipay

评论 (0)

取消