无法读取JSX中的数组长度(React-Redux)

时间:2020-08-06 22:22:47

标签: arrays react-redux mapstatetoprops

足够简单的问题。我正在尝试读取此对象数组内的“ length”变量的值(请参见图1)

enter image description here

我的密码

const mapStateToProps = (state, ownProps) => {

    const lecture = state.firestore.ordered.lectures;
    const length = lecture.length;

    return {

        length: length,

    }
}

1 个答案:

答案 0 :(得分:0)

在写此问题时,我找到了解决方案:

解决方案代码

const mapStateToProps = (state, ownProps) => {


    const length = state.firestore.ordered.lectures && state.firestore.ordered.lectures.length;



    return {

        length: length,

    }
}