提问者:小点点

react native,无法对未安装的组件执行react状态更新


当我登录到我的i.m Get memory leak时,警告“无法对未安装的组件执行React状态更新。这是一个no op,但它表示应用程序中存在内存泄漏。若要修复,请取消useEffect cleanup函数中的所有订阅和异步任务”

登录功能

 const onSubmit = () => {
    const checkValid = isValidData();
    if (checkValid) {
      updateState({isLoading: true});
      action
        .login_with_password({
          country_code: countryCode,
          phone_no: phoneNumber,
          password,
        })
        .then(res => {
          console.log(res);
          if (!res.data.ref_code_verified) {
            navigate(strings.nav_referalcode);
          } else if (!res.data.profile_completed) {
            navigate(strings.nav_profile_setUp);
          } else if (res.data.interest_ids.length == 0) {
            navigate(strings.nav_interest);
            updateState({isLoading: false});
          } else if (res.data.strongest_subject == null) {
            navigate(strings.nav_subject);
            updateState({isLoading: false});
          } else if (!res.data.competitive_exam) {
            navigate(strings.nav_exam);
            updateState({isLoading: false});
          } else if (res.data.purchased_plans.length == 0) {
            navigate(strings.nav_purchase_plan);
            updateState({isLoading: false});
          }

          updateState({isLoading: false});
        })
        .catch(error => {
          console.log(error);
          showError(error.message);
          updateState({isLoading: false});
        });
    }
  };

在我的主页上没有使用效果。为什么我会出现这个错误?有人知道怎么修吗?


共1个答案

匿名用户

您可能会看到此警告,因为您正在排队将路由转换到应用中的另一个页面,并排队等待状态更新。转换发生,组件卸载,然后React尝试更新状态,但不能。

从应用程序路径中删除状态更新调用。返回导航,在不满足任何条件的情况下保持上次状态更新。

const onSubmit = () => {
  const checkValid = isValidData();
  if (checkValid) {
    updateState({ isLoading: true });
    action
      .login_with_password({
        country_code: countryCode,
        phone_no: phoneNumber,
        password,
      })
      .then(res => {
        console.log(res);
        if (!res.data.ref_code_verified) {
          return navigate(strings.nav_referalcode);
        } else if (!res.data.profile_completed) {
          return navigate(strings.nav_profile_setUp);
        } else if (!res.data.interest_ids.length) {
          return navigate(strings.nav_interest);
        } else if (res.data.strongest_subject === null) {
          return navigate(strings.nav_subject);
        } else if (!res.data.competitive_exam) {
          return navigate(strings.nav_exam);
        } else if (!res.data.purchased_plans.length) {
          return navigate(strings.nav_purchase_plan);
        }

        updateState({ isLoading: false });
      })
      .catch(error => {
        console.log(error);
        showError(error.message);
        updateState({ isLoading: false });
      });
  }
};