首页 > 解决方案 > 无法读取 JSX 中数组的长度(React-Redux)

问题描述

很简单的问题。我正在尝试读取此对象数组中“长度”变量的值(参见图 1)

在此处输入图像描述

我的代码

const mapStateToProps = (state, ownProps) => {

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

    return {

        length: length,

    }
}

标签: arraysreact-reduxmapstatetoprops

解决方案


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

解决方案代码

const mapStateToProps = (state, ownProps) => {


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



    return {

        length: length,

    }
}

推荐阅读