我在做react-rails應用程序。我得到了這個奇怪的錯誤。似乎它是反應本身的問題。下面是我得到這個錯誤上下文:無法讀取null屬性'_currentElement'
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>
)
}
});
和我ChronicsList樣子:
var ChronicsList = React.createClass({
render:function(){
console.log(this.props.chronics);
var chronics = this.props.chronics.map(function(chronic){
return(
<Chronic name={chronic.name}/>
)
});
return(
<ul>
{chronics}
</ul>
)
}
});
從日誌中我看到這一點:
undefined
can not read property .map of undefined
Cannot read property '_currentElement' of null
我從他們身上看到最初狀態chronics爲null,因此.map不能被使用。然後當Ajax調用setsState我ChronicBox重新渲染和chronics包含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"
}],
}
所以chronics在ChronicForm現在定義。但我沒有看到裏面ChronicForm props.chronics這些日誌,而不是我得到的錯誤:
Cannot read property '_currentElement' of null
有什麼辦法來解決這個問題呢?我看到這是一個反應問題,但它被關閉了。