Reactjs 无法读取属性'_currentElement';空的

Reactjs 无法读取属性'_currentElement';空的,reactjs,Reactjs,我正在做react rails应用程序。我犯了一个奇怪的错误。似乎这是反应本身的问题。以下是我获得此错误的上下文: var ChronicBox = React.createClass({ getInitialState: function(){ return {chronics: []} }, componentWillMount: function(){ $.ajax({ // some ajax call that setsSta

我正在做react rails应用程序。我犯了一个奇怪的错误。似乎这是反应本身的问题。以下是我获得此错误的上下文:

var ChronicBox = React.createClass({

    getInitialState: function(){
      return {chronics: []}
    },
    componentWillMount: function(){
      $.ajax({
     // some ajax call that setsState of chronicles
      });
    },
    render: function(){
        return(
            <div className="chronics">
                <ChronicsList chronics={this.state.chronics.chronicsList} />
            </div>
        )
    }
});
从这些我看到最初的状态时间是空的,因此.map不能使用。然后,当ajax调用setState时,my ChronicBox会重新呈现,并且Chronic包含的json信息为folosw:

{
    "chronicsList": [{
        "id": 1,
        "name": "some allergy",
        "patient_id": 2,
        "created_at": "2016-02-11T19:05:33.434Z",
        "updated_at": "2016-02-11T19:05:33.434Z"
    }, {
        "id": 2,
        "name": "one more allergy",
        "patient_id": 2,
        "created_at": "2016-02-11T19:06:04.384Z",
        "updated_at": "2016-02-11T19:06:04.384Z"
    }],
}
所以现在,慢性病是以慢性形式定义的。但是我没有在ChronicForm中看到那些props.chronics的日志,而是得到了错误:

Cannot read property '_currentElement' of null

有没有办法解决这个问题?我在react中看到这是一个问题,但它已关闭。

我认为当任何组件的渲染函数抛出错误时,通常都会出现此错误。修复
.map
错误,此错误可能会消失

您可以使用默认道具修复
.map
问题:

getDefaultProps: function() {
  return {
    chronics: []
  };
}

正如您所注意到的,
this.props.chronics
为空,因此映射将不起作用。因此,react无法渲染组件。当数据重新加载并且
chronics
是一个数组时,react这次能够成功调用render方法;但是,当它尝试将当前虚拟DOM树与前一个虚拟DOM树进行比较时,由于无法找到前一个DOM树,因此失败

解决方案是在初始加载时传递一个数组。因此,您必须将根组件的初始状态设置为:

getInitialState: function(){
  return {
    chronics: {
      chronicsList: []
    }
  }
},

在三元操作中尝试渲染
null
时,我遇到了相同的错误。呈现空字符串解决了以下问题:

{this.props.list.map((n) => {
    return <option value={n.key} key={n.key}>
               {n.title}{n.isDraft ? ' - (draft)' : null}
           </option>;
    })
}
{this.props.list.map((n)=>{
返回
{n.title}{n.isDraft?'-(草稿)':null}
;
})
}
getInitialState: function(){
  return {
    chronics: {
      chronicsList: []
    }
  }
},
{this.props.list.map((n) => {
    return <option value={n.key} key={n.key}>
               {n.title}{n.isDraft ? ' - (draft)' : null}
           </option>;
    })
}