【发布时间】:2015-01-27 08:19:35
【问题描述】:
我想在我的 ajax 请求完成后渲染我的组件。
下面你可以看到我的代码
var CategoriesSetup = React.createClass({
render: function(){
var rows = [];
$.get('http://foobar.io/api/v1/listings/categories/').done(function (data) {
$.each(data, function(index, element){
rows.push(<OptionRow obj={element} />);
});
return (<Input type='select'>{rows}</Input>)
})
}
});
但我收到以下错误,因为我在我的 ajax 请求的 done 方法中返回渲染。
Uncaught Error: Invariant Violation: CategoriesSetup.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.
有没有办法在开始渲染之前等待我的 ajax 请求结束?
【问题讨论】:
-
另外,一个小小的挑剔但在 render() 例程中进行数据检索并不是一个好主意。保持 render() 进行渲染并抽象出其余部分。此外,您可能只想获取该数据一次,而不是每次渲染组件时。
标签: javascript ajax asynchronous reactjs jquery-deferred