首页 > 解决方案 > 无法对未安装的组件执行 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 方法中的所有订阅和异步任务”

标签: javascriptreactjscomponents

解决方案


您需要清理订阅。您可以使用它AbortController来实现。看看这个帖子


推荐阅读