React报错:Can‘t perform a React state update on an unmounted component. This is a no-op, but it indica

谁说胖子不能爱 提交于 2020-12-06 11:31:43

React报错: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.

今天在开发的时候功能没有问题,但是控制台报这个错误,然后在网上搜了一下,解决了,在这里记录一下,以后方便看。

问题

在这里插入图片描述

解决方案

将生命周期componentDidMount修改为componentWillUnmount就可以了

原因:不能在组件销毁后设置state,防止出现内存泄漏的情况,

组件都被销毁了,就不能设置state了

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!