【发布时间】:2017-06-15 06:54:27
【问题描述】:
action.js
export function getLoginStatus() {
return async(dispatch) => {
let token = await getOAuthToken();
let success = await verifyToken(token);
if (success == true) {
dispatch(loginStatus(success));
} else {
console.log("Success: False");
console.log("Token mismatch");
}
return success;
}
}
component.js
componentDidMount() {
this.props.dispatch(splashAction.getLoginStatus())
.then((success) => {
if (success == true) {
Actions.counter()
} else {
console.log("Login not successfull");
}
});
}
但是,当我使用如下所示的 async/await 编写 component.js 代码时,会出现以下错误:
Possible Unhandled Promise Rejection (id: 0): undefined is not a function (evaluating 'this.props.dispatch(splashAction.getLoginStatus())')
component.js
async componentDidMount() {
let success = await this.props.dispatch(splashAction.getLoginStatus());
if (success == true) {
Actions.counter()
} else {
console.log("Login not successfull");
}
}
如何等待 getLoginStatus() 然后执行其余语句? 使用 .then() 时一切正常。我怀疑我的 async/await 实现中缺少某些东西。试图弄清楚这一点。
【问题讨论】:
-
你有没有解决过这个模式?如果可以,您可以在这里发布答案吗?
-
你为什么要在一个 redux 连接的 react 组件中等待一个 promise?这打破了数据单向模式
标签: reactjs react-native react-redux redux-thunk ecmascript-2017