1
我有以下異步功能用於通過REST服務驗證有效的用戶憑證;異步功能中的反應設置狀態
async doesLoginExist(regCode) {
if (loginCode.match(loginCodePattern)) {
try {
await LoginService.getUserByLoginCode();
return {};
} catch (err) {
if (err.status === 404) {
return {
error: 'Your login code is not recognized.', success: null
};
}
return {
error: 'Service is temporarily unavailable.', success: null
};
}
}
return {};
}
但是,當我嘗試在此函數中設置某些狀態時;
async doesLoginExist(regCode) {
await this.setState({ canProceed: false });
if (loginCode.match(loginCodePattern)) {
try {
await LoginService.getUserByLoginCode();
await this.setState({ canProceed: true });
return {};
} catch (err) {
if (err.status === 404) {
return {
error: 'Your login code is not recognized.', success: null
};
}
return {
error: 'Service is temporarily unavailable.', success: null
};
}
}
return {};
}
似乎不再對我{錯誤,成功}對象正確地返回給調用者,這最終被用作屏幕上顯示的驗證消息。另一個小問題是在輸入登錄代碼的文本框上輸入autoFocus也不起作用。
有沒有其他方法我應該在這個函數內設置狀態?
你爲什麼在this.setState上使用await? – linasmnew
嗯,我想這不是必須的。但是,當我把它拿出來的同樣的問題。 – deanmau5
您還沒有足夠清楚地描述問題,「不再似乎正確地返回我的{錯誤,成功}是什麼意思? – linasmnew