报错: 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.
Goole 翻译: 无法在已卸载的组件上执行React状态更新。 这是空操作,但它表明应用程序中发生内存泄漏。 要解决此问题,请在componentWillUnmount方法中取消所有订阅和异步任务。
react项目切换路由的时候, 一直报这个错误,
最后在 点击按钮切换路由的那个页面,加了个卸载生命周期 componentWillUnmount
componentWillUnmount() {
this.setState = () => {
return;
};
};
好了…
在切换React路由时遇到一个问题,即从一个组件跳转到另一个组件后,前一个组件的异步更新仍在进行,从而触发了'Can’t perform a React state update on an unmounted component.'的错误。这提示存在内存泄漏。解决方法是在组件的componentWillUnmount生命周期方法中取消所有订阅和异步任务。通过在触发路由切换的按钮所在页面添加componentWillUnmount生命周期方法,成功解决了这个问题。
1670

被折叠的 条评论
为什么被折叠?



