我目前正在尝试学习ReactJS,但很难找到一致的文档(即使官方文档看起来有点混乱),特别是关于AJAX的部分,似乎都是拼凑而成的。最终我使用了JQuery的$.get方法,这似乎有点作弊。
我有下面的代码,但想知道是否有更简洁的处理简单AJAX调用的方法?我也在componentDidMount中触发它,这似乎是最合适的,因为它是生命周期的一部分。
欢迎任何帮助!
getInitialState: function () {
return {
numberOfItems: 0,
items: []
};
},
componentDidMount: function () {
$('#MyContainer .loading').show();
$.get(this.props.source, function (result) {
var response = result.response;
this.setState({
numberOfItems: response.list.numberOfItems,
items: response.list
});
}.bind(this))
.done(function () {
$('#MyContainer.loading').hide();
});
},
render: function() {
return(
//Stuff here
)
};
ReactDOM.render(
<RecommendationsList source="/my/api/call/here"/>,
document.getElementById('MyContainer')
);