我有以下代码,nextProps.ad.submission.content
是异步数据
componentWillReceiveProps(nextProps) {
if(this.state.body !== get(nextProps.ad.submission.content, '')){
this.setState({
body: nextProps.ad && nextProps.ad.submission && nextProps.ad.submission.content
})
}
}
get
来自lodash,为什么我收到Cannot read property 'content' of undefined
的错误?这不是nextProps.ad && nextProps.ad.submission && nextProps.ad.submission.ad
吗?
答案 0 :(得分:0)
错误在此行
if(this.state.body !== get(nextProps.ad.submission.content, '')){
表示在nextProps.ad
中您没有名为submission
的属性,这意味着nextProps.ad.submission
未定义,并且您尝试访问content
的属性undefined
检查nextProps.ad.submission
是否未首先定义
componentWillReceiveProps(nextProps)
{
if(nextProps.ad.submission !== undefined)
{
if (this.state.body !== get(nextProps.ad.submission.content, ''))
{
this.setState({
body: nextProps.ad && nextProps.ad.submission && nextProps.ad.submission.content
})
}
}
}