Why doesn’t my React Native app update its UI only when I make an axios request?

I am testing offline behavior and errors in a React Native app. I have a modal that is shown and hidden in its parent like this:

    return (
        ...
        {showTranslationProgress && (
          <TranslatingRecipesView
            ...
            setShowTranslationProgress={setShowTranslationProgress}
            ...
          />
        )}
        ...
   )

This modal shows a progress bar on mount that is then closed once the job is finished. It works as expected except in the case of the last else block here in the modal:

 } catch (err) {   
      setShowTranslationProgress(false);
      if (err.message === noInternet) {
         Alert.alert(noInternet, noInternetMessage);
      } else {
         Alert.alert(ErrorCodeOthers);
      }

      i18n.changeLanguage(prevGlobalLanguageCode);
   }

In the first error case, I am throwing a new Error() and the progress bar hides when the alert pops up, as expected. The last case catches all other errors and when it is triggered, the progress bar does not hide and I can see it behind the alert. The only difference I have been able to find is that the type of error is an Axios error. Though even when I just throw a regular error instead of passing along the Axios error, it still happens, so I am stumped.

All logs in the parent and in the modal seem to confirm that the state is being properly updated. I have tried:

  1. Moving setShowTranslationProgress(false) around in different places inside the catch block.
  2. Putting the alerts in a setTimeout.
  3. Adding a finally block and calling setShowTranslationProgress(false) there.
  4. Wrapping the Axios error in a new Error() at the place it is thrown.
  5. Not passing along the Axios error and just throwing a normal Error like I do in the other cases.
  6. Forcing a re-render of the parent and modal.
  7. Decreasing the timeout of the axios request.
  8. Using the modal’s visible property instead of the conditional rendering in the parent.

Here is where the error happens. If the ‘No internet’ block is triggered, the progress bar hides as expected, if the catch block inside that is triggered, it doesn’t. Notice I have commented out throwing the Axios error and am just throwing a standard Error:

static async getUnitOfMeasureTranslation(
    unitOfMeasureList: UnitOfMeasureModel[],
    targetLanguage: string
  ) {
    const unitOfMeasureData: any[] = [];
    unitOfMeasureList.map((unitOfMeasure) => {
      unitOfMeasureData.push({Text: unitOfMeasure.description});
    });
    const state = await NetInfo.fetch();
    if (state.isConnected) {
      try {
        const response = await this.getTranslation(
          unitOfMeasureData,
          targetLanguage
        );
        const translatedData = response.data;
        const updatedTranslatedData = unitOfMeasureList.map(
          (unitOfMeasure,index) => {
            return {
              ...unitOfMeasure,
              description: translatedData[index].translations[0].text,
              unique_id: unitOfMeasure.uom_id + '_' + targetLanguage,
              language: targetLanguage,
            };
          }
        );
        await createData(SchemaNames.UnitOfMeasureTranslationSchema,{
          title: 'UnitOfMeasure',
          data: updatedTranslatedData,
        });
        const mergedUnitOfMeasure = await this.mergeUnitOfMeasureTranslation();
        return mergedUnitOfMeasure;
      } catch(e) {
        console.log(`ERROR TRANSLATING UNIT OF MEASURE TO ${targetLanguage}:`,e);
        // throw e
        throw new Error(translationNoInternetMessage);
      } 
    } else {
      throw new Error(noInternet);
    }
  }

UPDATE: If I throw an error right before the axios request, inside the try block, it works as expected. If I throw one right after, it doesn’t. This leads me to believe it has something to do with the request itself, which is baffling to me.

I didn’t want to overwhelm with code as there is a lot. Since it works in most cases I thought isolating it would be most helpful. I can supply more code if needed though.

This is the only thing I found that helped. The way I understand it is that Axios continues to do work after it throws an error and this can sometimes overwhelm React’s state updates. It feels hacky but it’s all I could get to work. Anyone who knows more about this please comment.

 } catch (err) {
     if (err.message === noInternet) {
        setShowTranslationProgress(false);
        Alert.alert(noInternet, noInternetMessage);
     } else {
        // This allows the progress bar to hide. Axios request
        // was causing it to stay visible behind the alert.
        await new Promise(resolve => setTimeout(resolve, 500));
        setShowTranslationProgress(false);
        Alert.alert(ErrorCodeOthers);
     }

     i18n.changeLanguage(prevLanguageCode);
   }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật