【发布时间】:2017-06-18 23:43:46
【问题描述】:
我已经设置了名为 data 的状态,并在 getInitialState() 中将其声明为一个空数组。此外,我进行了 ajax 调用并在 componentDidMount() 中获取了 JSON 作为回报。
如何使用 setState 方法将多个 JSON 请求推送到名为 data 的数组?
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调用,你确定结果是数组吗?
标签: javascript jquery ajax reactjs