【发布时间】:2016-04-08 15:20:04
【问题描述】:
有问题的代码如下。我在componentDidMount 中有一个异步UserActions 调用,然后我立即从这个操作填充的UserStore 中查找用户信息。显然,我不能依赖 UserStore.isLargeAccess() 的定义。将依赖于操作的代码放在回调中是最好的约定,还是我错过了一些更大的设计选择?
componentDidMount() {
this.changeListener = this._onChange.bind(this);
UserStore.addChangeListener(this.changeListener);
OrganizationStore.addChangeListener(this.changeListener);
// Fetch user info
UserActions.get(AuthStore.username);
// UserStore.isLargeAccess() is undefined here,
// because the previous action has not finished.
if (UserStore.isLargeAccess()) {
OrganizationActions.getUsers(this.state.organization);
}
if (UserStore.isGlobalAccess()) {
OrganizationActions.getOrganizations();
}
}
【问题讨论】:
标签: javascript reactjs reactjs-flux