action.js如何異步/等待redux-thunk操作?
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");
}
});
}
然而,當我寫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()時,一切正常。我懷疑我的異步/等待實現中有什麼缺失。試圖弄清楚。
你有沒有得到這種模式?如果是的話,你可以在這裏發佈答案嗎? – ajonno
你爲什麼要等待redux連接反應組件中的承諾?這是打破數據單向模式 – Aaron