学无先后,达者为师

网站首页 编程语言 正文

Warning: Can‘t perform a React state update on an unmounted component.

作者:菜菜Caro 更新时间: 2021-12-16 编程语言

报错如下:

在这里插入图片描述

Warning: 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组件已经被销毁,无法对已卸载的组件执行React状态更新。这是一个no-op,但它表示应用程序中存在内存泄漏。


解决方案:

在组件componentWillUnmount销毁的时候将异步方法取消

componentWillUnmount() {
  this.setState = () => false;
}

原文链接:https://blog.csdn.net/Caroline_cai/article/details/122099290

栏目分类
最近更新