javascript - 无法在 React js 和 firestore 中对未安装的组件执行 React 状态更新

标签 javascript reactjs google-cloud-firestore

我知道这些论坛里有这个问题,但是我用了那些方法,错误依旧:

我使用 componentDidMount 从 firestore 加载数据,并使用 componentWillUnmount 将变量 _isMounted 应用到 f​​alse。

Error image

我在表中的代码是这样的:

import React, { Component } from 'react';
import DashboardNavBar from '../../components/navbar/navbarDashboard';
import TableBox from './boxTable';
import TableSalary from './employs';
import TableExpenses from './expensesTable';
import firebase from '../../config/fbConfig';
import Grid from '@material-ui/core/Grid';
import CircularProgress from '@material-ui/core/CircularProgress';

export default class TableCount extends React.Component {

    constructor(props) {
        super(props);
    }

    render() {
        if (verifyBusinessData()) {
            return (
                <Grid
                container
                spacing={0}
                direction="column"
                alignItems="center"
                justify="center"
                style={{ minHeight: '100vh' }}
                >
                    <CircularProgress />
                </Grid>
            );
        }  else {
            return (
                <div className="tables-page">
                    <DashboardNavBar />

                    <div className="container-panels">

                        <h2 className="titlePage">Tablas</h2>

                        { /*<div className="tables-div-cards">
                            { this.renderUsersCards() }
                        </div> */}

                        <div className="tables-container table-top">

                            <div className="tables-container-first">
                                <div className="tables-first">
                                    <TableBox />
                                </div>

                                <div className="tables-second">
                                    <TableSalary />
                                </div>
                            </div>

                            <div className="tables-third">
                                <TableExpenses />
                            </div>
                        </div>
                    </div>
                </div>
            );
        }
    }
}

在 tableBox 中:

import React from 'react';
import MaterialTable from 'material-table';

export default class MaterialTableDemo extends React.Component {

    _isMounted = false;

    constructor(props) {
        super(props)

        this.state = {
            data: [],
            isLoading: true,
        }

    componentDidMount() {
        this._isMounted = true;

        if (this._isMounted)
        {
            this.loadSalary();
        }
    }

    componentWillUnmount() {
        this._isMounted = false;
    }

    loadSalary() {
        // Load data from firestore
    }

    render() {

        if (!this.state.isLoading) {
        return (
            <div>

            <div className="table-icon-div">
                <div className="table-icon turquoise-icon"><img src={ require('../../img/icons/expenses-white.png') } /></div>
                <h3 className="table-icon-title">Caja</h3>
            </div>

            // render the material table but skip the unnecessary code
            <MaterialTable
                style={stiles.title}
                icons={tableIcons}
                title="Caja"
                columns={this.state.columns}
                data={this.state.data}
            > 
            </MaterialTable>  

            </div>
        );
        } else {
            return (
            <div>Loading...</div>
            );
        }
    }
}

我应用了方法 componentWillUnmount 但不起作用。

最佳答案

如果您使用 loadSalary 中的 promise 异步设置状态,则可能是您的组件在 promise 解决之前已卸载。如果是这种情况,您应该在设置状态之前检查 this._isMounted,例如:

loadSalary() {
  fetchSalary().then(data => {
    if (this._isMounted) {
      this.setState({ data });
    };
  });
}

我在这里做了一些假设,如果您更新代码以显示 loadSalary 实现,这会有所帮助。

关于javascript - 无法在 React js 和 firestore 中对未安装的组件执行 React 状态更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384765/

相关文章:

node.js - 如何修复 "× TypeError: Object(...) is not a function"?

java - 在 Firestore 中找不到文档数据,返回 null

android - 将数据从共享托管网页写入 firebase firestore 的 Web 应用程序

firebase - 软件包 cloud_firestore 没有匹配 >=0.7.0 <0.8.0 的版本

javascript - 我不知道为什么我的 min 函数不起作用,它一直给我 0 作为答案

javascript - Reactjs 和 Nodejs 在 VS Code 中的通信

javascript - 继承方式不同

node.js - React 和 Socket.io

javascript - 每个数组值旁边的删除按钮

javascript - 如何防止 fs.writeFile 覆盖现有文件?