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

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 a useEffect cleanup function.
原因就是报错中所说:无法对已卸载的组件执行react状态更新。

假设现在我们正在A页面调接口请求数据,请求成功后setState()更新数据,这是没有问题的。

但是如果A页面在请求接口途中,切换路由跳转至B页面,这时候A页面的数据请求还在进行中,等到数据返回的时候,调用setState()发现A组件被卸载了,就会抛出以上异常。

posted @ 2022-12-07 11:57  Arborblog  阅读(341)  评论(0编辑  收藏  举报