我有一个场景,我将数据从减速器传递到我的react状态。
数据:
{
    "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'){
但这也不起作用,我相信这是因为博客未定义。
