【发布时间】:2017-04-05 17:56:18
【问题描述】:
我有一个场景,我将数据从减速器传递到我的反应状态。
数据:
{
"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 谢谢!我将代码固定为 blog.content 而不仅仅是内容,这是您的意思吗?我仍然遇到同样的错误。
标签: reactjs components redux undefined