在Reac开发中,可能会经常遇见这个报错: index.js:1 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.
常见场景
- 在组件上进行axios的异步请求,请求之后setState存储数据
- 然后在组件之间快速切换组件
然后就会出现这个报错了,报这个错主要是因为,不能在组件销毁后设置state,防止出现内存泄漏的情况!
解决办法
1.比较简单粗暴的处理方法是,直接设置componentWillUnmount
componentWillUnmount = ()=>{
this