我已經設置了名爲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