说我有这个:
const mapStateToProps = ({
amount: {
monthly: { cost },
},
}) => ({
amount,
period,
interval,
})
它提示它无法读取 null 的 cost
这是因为 something monthly 是 null
如果 monthly 为 null,我是否可以仅针对 cost
返回 null?
我试过做 cost: cost || null
或其他内容但无济于事
有什么想法吗?
最佳答案
可能 monthly
是 undefined
。您可以尝试在解构过程中设置默认值:
const mapStateToProps = ({
amount: {
monthly: { cost } = { cost: null },
},
}) => ({
// ...
});
关于javascript - 无法读取 null 的属性,mapStateToProps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51612012/