reactjs - React - 内存泄漏,ComponentWillUnmount / Axios 的问题
问题描述
我正在执行一个使用 componentDidMount 启动的 API 调用,但是当用户加载一个新组件时,会有一个潜在内存泄漏的通知,请参阅下面的消息。我研究了不同的解决方案,但还没有发现任何可行的方法,我想知道是否可以使用 componentWillUnmount 在此特定组件中修复此问题,或者是否可以在 axios 调用本身中更好地处理。
无法对未安装的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请取消 componentWillUnmount 方法中的所有订阅和异步任务。
componentDidMount() {
this.loadBackground();
this.getUpdatedWeather();
this.getNewMartianPhotos();
}
checkMartianPhotos = () => {
if (this.state.martianMotion) {
console.log('still shooting');
this.getNewMartianPhotos();
} else {
return console.log('done now');
}
};
getNewMartianPhotos = () => {
let loadedImage = '';
let loadedInfo = '';
let loadedMeta = '';
let totalImage;
API.getMarsPhotos().then(data => {
// console.log(data.data);
// console.log(
// data.data.collection.items[this.state.martianCount].data[0].photographer
// );
// console.log(
// data.data.collection.items[this.state.martianCount].data[0].description
// );
// console.log(
// data.data.collection.items[this.state.martianCount].links[0].href
// );
totalImage = data.data.collection.items.length;
loadedImage =
data.data.collection.items[this.state.martianCount].links[0].href;
loadedInfo =
data.data.collection.items[this.state.martianCount].data[0].description;
loadedMeta =
data.data.collection.items[this.state.martianCount].data[0]
.photographer;
this.setState({
martianImage: loadedImage,
martianDescription: loadedInfo,
martianMeta: loadedMeta,
martianCount: this.state.martianCount + 1
});
if (this.state.martianCount < totalImage) {
console.log(
`shooting off, image count now ${this.state.martianCount} against ${totalImage}`
);
setTimeout(this.checkMartianPhotos, 10000);
}
});
};
componentWillUnmount() {
clearTimeout(this.checkMartianPhotos);
}
-------------------------------------
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
getMarsPhotos: () =>
axios
.get('https://images-api.nasa.gov/search?q=martian', {
cancelToken: source.token
})
.catch(function(thrown) {
if (axios.isCancel(thrown)) {
console.log('request canceled', thrown.message);
} else {
console.log('there is an error that needs to be handled');
}
})
解决方案
如错误所示,您的组件在卸载后调用 setState。这是因为您错误地清除了超时。您应该执行以下操作以正确清除卸载超时。
this.id = setTimeout(this.checkMartianPhotos, 10000);
然后清除
clearTimeout(this.id)
在组件WillUnmount
此外,尝试将异步逻辑(api 调用)移出组件。
请参阅此内容以了解如何在对已卸载组件进行 API 调用后停止设置状态。
推荐阅读
- amazon-web-services - 如何使用 AWS 设置 no-www?
- java - IntelliJ 配置中的命名策略映射标记是什么意思?
- java - 如何使用 Jackson 将 JSON 键值数组动态映射到子对象?
- c# - 防止扫描仪冻结程序
- powerbi - 将选项卡导出为单独的报告
- spring-boot - 多模块 Gradle 项目 - 从 Spring-Boot 1.5 迁移到 2.1
- c - 如何为C中的链表头分配空间?
- angular - Karma 在测试结果上渲染 html
- reactjs - 使用 React 和 Redux 连接在一起的父子组件中的值未定义。应用程序崩溃
- swift - 如何以编程方式使没有宽度的自定义按钮居中