首页 > 解决方案 > 刷新页面我得到 TypeError: Cannot read property 'Location' of undefined

问题描述

您好,我有一个反应应用程序,它运行良好。所有路线,页面加载。一切都很完美。但是,当进入 FarmProperty 页面并尝试重新加载它 (F5)时,TypeError: Cannot read property 'tenant_account' of undefined会发生错误。

FarmProperty.js

import { connect } from 'react-redux';
import * as actions from '../../store/actions/farmProperty';

class FarmProperty extends Component {

    state = {
        page: 0,
        limit: 15,
        msg: "",
        erro: "",
        success: "",
        loading: false,
        openModal: false,
        id_delete: "",
        tenant_account_id_delete: "",
        apiData: false
    }

    
     componentDidMount() {
        this.getFarmProperties();
    }

     componentDidUpdate(nextProps) {
        if (!this.props.user && nextProps.user) this.getFarmProperties();
        this.receiveApiData();
    }


     getFarmProperties() {
        const { page, limit } = this.state;
        const { farm_properties, user } = this.props;
        //console.log(user);
        
        this.props.getFarmProperties(user.tenant_account.id, page, limit);
        
        if (this.props.location.state) {
            this.setState({ msg: this.props.location.state.msg });
            this.props.location.state.msg = "";
        }
        if (farm_properties === "undefined") return null;
    }

     receiveApiData() {
        if (typeof this.props.farm_properties !== "undefined" && this.props.farm_properties !== null
            && !this.state.apiData && this.props.farm_properties.data.currentPageNumber === this.state.page) {
            this.setState({ apiData: true });
        }
    }

    render() {
        return (
            <>

            </>
        )
    }
}

const mapStateToProps = state => ({
    farm_properties: state.farmProperty.farm_properties,
    user: state.auth.user
})

export default connect(mapStateToProps, actions)(FarmProperty);

我相信问题出在功能上 this.props.getFarmProperties (user.tenant_account.id, page, limit);

当我传递user.tenant_account.id参数时。这个参数是登录到应用程序的用户,我需要获取这些信息,但是如果有其他方法可以获取这些信息,我将不胜感激。

标签: reactjsreact-redux

解决方案


推荐阅读