javascript - 无法对未安装的组件执行 React 状态更新。这是一个无操作
问题描述
我有以下代码:
export class ProductsCategory extends Component {
constructor() {
super();
// Title page
document.title = "products category";
// Varables url
let { getAllRows, changeOrder, changeTitle, changeIsActive } = new APIs().ProductsCategoryApi();
this.state = {
bindData: [],
loading: true,
getAllRowsUrl: getAllRows,
changeTitleUrl: changeTitle,
changeOrderUrl: changeOrder,
changeIsActiveUrl: changeIsActive,
};
// Bind table
this.bindTable();
}
bindTable = () => {
// Bind table
(async () => {
let response = await fetch(this.state.getAllRowsUrl);
// if HTTP-status is 200-299
if (response.ok) {
this.setState({
bindData: await response.json(),
loading: false
});
return;
}
Notify.bindData(false);
})();
}
reloadTable = () => { }
componentDidUpdate() {
this.bindTable();
}
render() { Some code };}
更改页面时,我在控制台选项卡中看到此错误:
“无法对未安装的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请取消 componentWillUnmount 方法中的所有订阅和异步任务”
解决方案
您需要清理订阅。您可以使用它AbortController
来实现。看看这个帖子
推荐阅读
- java - 与调用等效 JAR 相比,访问 Web 服务的成本更高
- node.js - Homebridge如何在函数中调用服务函数?
- javascript - jasmine-node 看不到测试
- android - 切换片段时 ItemTouchHelper 手势后崩溃
- angularjs - angularjs中的checkAll复选框不起作用
- laravel - 如何在 Laravel 单元测试中模拟多对多关系
- python - 如何仅提取文件名?
- java - Java Optional 接口:Optional 对象链上的嵌套条件
- c# - 在字符串中定义的我的类名的实例列表
- react-native - 在 webview react_native 中处理单击按钮或取消划线文本