简体   繁体   English

如何修复mapStateToProps避免访问未定义的状态键而无需手动检查?

[英]How to fix mapStateToProps from accessing an undefined state key without checking manually?

I am creating a single page web application with React, with the state stored using Redux. 我正在用React创建一个单页Web应用程序,其状态使用Redux存储。 When I access something like state.workspace.guest.data in mapStateToProps() it causes an exception crashing the application. 当我在mapStateToProps()访问诸如state.workspace.guest.data类的state.workspace.guest.data ,它将导致异常,使应用程序崩溃。

It appears mapStateToProps() is invoked before componentDidMount() , which invokes this.props.getGuests() action that actually causes state.workspace.guest to be initialized. 似乎mapStateToProps()之前被调用componentDidMount()这将调用this.props.getGuests()实际上导致动作state.workspace.guest被初始化。 The page loads properly when I manually enter the URL and press enter. 当我手动输入URL并按Enter时,页面将正确加载。 However, mapStateToProps() causes an exception when I navigate through the application, with state.workspace.guest being undefined . 但是,当我浏览应用程序时, mapStateToProps()导致异常,其中state.workspace.guestundefined

A possible solution would be to check if state.workspace.guest is defined. 一种可能的解决方案是检查state.workspace.guest是否已定义。 However, I feel there is a better way to do it? 但是,我觉得有更好的方法吗? The answers in the following link suggest to use selectors. 以下链接中的答案建议使用选择器。 I am not sure if this is the solution to my problem, given I am new to Redux. 考虑到我是Redux的新手,所以我不确定这是否是解决问题的方法。

react js mapStateToProps triggers Uncaught TypeError: Cannot read property 'map' of undefined 反应js mapStateToProps触发未捕获的TypeError:无法读取未定义的属性'map'

Here are the mapping functions. 这是映射功能。

function mapDispatchToProps(dispatch) {
    return bindActionCreators({
        getGuests: Actions.getGuests
    }, dispatch);
}

function mapStateToProps(state, ownProps) {
    return {
        guests : state.workspace.guest.data
    }
}

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(GuestSearchTable));

The componentDidMount() function which actually triggers an action which in turn loads the data. componentDidMount()函数实际上触发一个动作,该动作又会加载数据。

    componentDidMount() {
        this.props.getGuests();
    }

Here is the reducer. 这是减速器。

const initialState = {
    data : []
};

function guestReducer(state = initialState, action) {
    switch (action.type) {
        case Actions.GET_GUESTS: {
            return {
                ...state,
                data : action.payload.data
            };
        }

        case Actions.GET_GUEST: {
            return {
                ...state,
                guest: action.payload.data
            };
        }

        case Actions.SAVE_GUEST: {
            return {
                ...state,
                guest: action.payload.data
            };
        }

        default: {
            return state;
        }
    }
};

I expect the initial value of the state.workspace.guest to be { data : [] } . 我希望state.workspace.guest的初始值为{ data : [] } But the actual value is undefined . 但是实际值是undefined The same problem appears in all the other pages. 在所有其他页面上也会出现相同的问题。

You are trying to access data from guest key. 您正在尝试从访客密钥访问数据。

function mapStateToProps(state, ownProps) {
    return {
        guests : state.workspace.guest.data
    }
}

I think you want to access state.workspace.data or you have to change key name to guests in reducer and the intialize state as follow: 我认为您想访问state.workspace.data,或者必须在reducer中将来宾的键名更改为initize,如下所示:

return {
   guests: state.workspace.guests
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM