我正在構建一個反應加密貨幣專案,并試圖找出加載程式在等待 api 回應時沒有出現的原因。所以我有一個呼叫 api 的 onClick 函式。
const handleShowSecondScreen = () => {
executeTransaction(price)
.then((res) => {
setIsLoading(true);
if (!res.status) {
setIsLoading(true);
} else if (res.status === 'OK') {
setIsLoading(false);
setSuccessfulTransactionInfo(res);
} else if (res.status === 'FAILED') {
setIsLoading(false);
setWidgetView(SCREEN_VIEWS.FAILED);
}
});
};
所以應該發生的是,一旦我單擊按鈕,加載程式應該會出現,直到有來自 api 的回應。但是,當我單擊按鈕時,沒有加載程式出現,它等待回應。不知道這里發生了什么。如果我在這里做錯了什么,請告訴我。謝謝!
uj5u.com熱心網友回復:
您應該在方法呼叫之前設定加載程式。
const handleShowSecondScreen = () => {
setIsLoading(true);
executeTransaction(price)
.then((res) => {
if (!res.status) {
setIsLoading(true);
} else if (res.status === 'OK') {
setIsLoading(false);
setSuccessfulTransactionInfo(res);
} else if (res.status === 'FAILED') {
setIsLoading(false);
setWidgetView(SCREEN_VIEWS.FAILED);
}
});
};
在您的情況下,您可以將代碼重構為這樣的內容。
const handleShowSecondScreen = () => {
setIsLoading(true);
executeTransaction(price)
.then((res) => {
if (!res.status) {
return
}
setIsLoading(false);
if (res.status === 'OK') {
setSuccessfulTransactionInfo(res);
} else if (res.status === 'FAILED') {
setWidgetView(SCREEN_VIEWS.FAILED);
}
});
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513515.html
