javascript - 无法读取 null 的属性,mapStateToProps

标签 javascript reactjs

说我有这个:

const mapStateToProps = ({
    amount: {
        monthly: { cost },
    },
}) => ({
    amount,
    period,
    interval,
})

它提示它无法读取 null 的 cost

这是因为 something monthly 是 null

如果 monthly 为 null,我是否可以仅针对 cost 返回 null?

我试过做 cost: cost || null 或其他内容但无济于事

有什么想法吗?

最佳答案

可能 monthlyundefined。您可以尝试在解构过程中设置默认值:

const mapStateToProps = ({
    amount: {
        monthly: { cost } = { cost: null },
    },
}) => ({
  // ...
});

关于javascript - 无法读取 null 的属性,mapStateToProps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51612012/

相关文章:

javascript - 检查 block 可见性

reactjs - React Styled Components 生成过多的 css

javascript - React Redux 骨架从状态返回未定义的值

javascript - 为什么我的 javascript 函数没有在我的 div 中被调用?

javascript - 如何将json文件内容加载到变量

javascript - React useState 不会在窗口事件中更新

reactjs - React Native 从状态数组中删除多个项目

reactjs - 如何在客户端隐藏 firebaseConfig 信息

javascript - Json.Parse 转义换行符

javascript - 使用 Jquery 函数的参数运行不同的结果