我已经设置了名为data的状态并将其声明为getInitialState()中的空数组。此外,我已经做了一个Ajax调用,并获得了componentDidMount()中返回的JSON。如何使用React中的setState将多个JSON请求推送到数组中
如何使用setState方法将多个JSON请求推送到称为数据的数据?
var Forecast = React.createClass({
getInitialState() {
return {
data: []
}
},
componentDidMount: function() {
this.serverRequest = $.get('http://api.openweathermap.org/data/2.5/weather?zip=3000,au&appid=005fa98ae858a29acf836ecdefac0411', function(result) {
var tempData = result;
this.setState({
// Is there any way to push multiple JSON into an array?
// below line of code is my attempt
data: tempData
});
}.bind(this));
}
...
}
'data [0]'是未定义的,因为数组是空的。不知道ajax调用,你确定结果是一个数组吗? – webdeb