[英]Loading React Component after AJAX
I have a ReactJS component that needs a response from an AJAX request before it can be rendered. 我有一个ReactJS组件,该组件需要AJAX请求的响应才能呈现。 I have tried to add a listener to the store, so that when it emits
loaded
, I call this.setProps(/*props*/)
on the component. 我试图向存储中添加一个侦听器,以便当它发出
loaded
,我在组件上调用this.setProps(/*props*/)
。 This doesn't work because the component has children that are also dependent upon that data being loaded. 这是行不通的,因为组件的子项也依赖于要加载的数据。
I have also tried using componentWillUpdate
and componentWillReceiveProps
. 我也尝试过使用
componentWillUpdate
和componentWillReceiveProps
。 I assumed that componentWillReceiveProps
was what I was looking for, which does work for the main view on it's own but it doesn't propogate the data down to the children. 我以为
componentWillReceiveProps
是我要找的东西,它本身可以用于主视图,但不会将数据传播给子级。
Any ideas? 有任何想法吗?
Problem solved 问题解决了
var Account = React.createClass({
/**
* @cfg {String}
*/
displayName: 'Account',
/**
* Get initial state of component
*/
getInitialState: function() {
return { user: null, accounts: null };
},
/**
* Set state of component when UserStore is loaded
*/
onUserStoreLoaded: function() {
this.setState({
user: UserStore.getUser(),
accounts: UserStore.getUserAccounts()
});
},
/**
* Perform logic when component is about to mount to
* the DOM
*/
componentWillMount: function() {
if (UserStore._user) {
this.onUserStoreLoaded();
} else {
UserStore.on('loaded', this.onUserStoreLoaded);
}
},
render: function() {
return (
<div className="col-sm-6 col-sm-offset-3">
<h1 className="text-center"> Account </h1>
<ConnectedAccounts accounts={this.state.accounts} />
</div>
);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.