我有一个场景,我将数据从reducer传递到我的反应状态。检查未定义在反应中
数据:
{
"id": 1,
"title": "Test",
"content": {
"body": "sdfsdf"
"image": "http://example.com"
}
}
使用componentWillRecieveProps,这完全适用于检索标题。
componentWillReceiveProps(nextProps) {
this.setState({
title: nextProps.blog.title,
})
}
但是,我很难检索嵌套字段。当我这样做:
componentWillReceiveProps(nextProps) {
console.log("new title is", nextProps.blog.title);
console.log("new body content is", nextProps.blog.content["body"]);
this.setState({
title: nextProps.blog.title,
body: nextProps.blog.content["body"]
})
}
我得到这个错误:
未定义体的错误消失我点击调试器和内容加载后。无论如何,我可以解决这个问题吗?
我试图检查未定义这样的:
if (typeof nextProps.blog.content["body"] != 'undefined'){
但是,这也不行,我相信这是因为博客是不确定的。
我觉得你的错误就在于,你的“身体”嵌套在“内容” – naomi
@naomi谢谢!我将我的代码固定到blog.content而不是仅仅是内容,那是什么意思?我仍然遇到同样的错误。 – lost9123193