有问题的代码如下。我在UserActions
内进行了异步componentDidMount
调用,之后我立即查看此操作填充的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();
}
}
答案 0 :(得分:1)
它应该如何工作(如果我理解你的流程):