【发布时间】:2018-09-08 22:55:15
【问题描述】:
我有一个高阶组件 (HOC),用于授权和刷新用户会话。
这是 RequireAuth HOC,它是 componentWillMount
componentWillMount() {
console.log('HOC componentWillMount');
// checks with authorizer that token is valid if not refresh token
getUserFromLocalStorage()
// update redux with new refresh token
.then(data => {
console.log('getUserFromLocalStorage')
this.props.setUserToReduxState(data);
console.log(
'user sucess retrieved and user setUserToReduxState: ',
data
);
})
.catch(() => {
logoutUserFromReduxState();
});
}
}
这是我的路线调用
<Route exact path="/devices" component={RequireAuth(Devices)} />
这是我的设备组件的 componentDidMount
componentDidMount() {
console.log('componentDidMount')
// calls api
this.loadData();
}
此子组件调用需要令牌的 API。 但是,当令牌 get 刷新时,它会在设备组件中调用 API get 之前刷新,但是从 HOC 中的 '.then' 承诺返回的 redux 操作不会在子组件中的 api 调用之前更新 redux 状态.
有没有办法在孩子尝试调用 API 之前确保令牌已经更新/redux 状态?
【问题讨论】:
-
在获取令牌时显示加载微调器或其他内容,而不是立即安装子组件
标签: javascript reactjs redux state amazon-cognito