如何使用 axios 清理 React-Native useEffect

发布于 2025-01-11 09:03:51 字数 995 浏览 0 评论 0原文

目前,我已经在功能组件中定义了一个 useEffect ,如下所示

useEffect(() => {
 (async function () {
   posts.current = await BlogConsumer.getBlogPosts();
   setLoading(false);
 })();

 return () => {
   BlogConsumer.call_controller.abort();
 };
}, []);

,其中 BlogConsumer 的定义如下。

class BlogConsumer {
  static posts = {};
  static call_controller = new AbortController();

  static async getBlogPosts() {
    await axios
      .get('https://nice.api', {
        signal: this.call_controller.signal,
      })
      .then(response => {
        // treatment for success
      })
      .catch(error => {
        // treatment for erros
      });

    return this.posts;
  }
}

export default BlogConsumer;

总体想法是,在组件的渲染中,我将从我的消费者调用一个静态方法,并且将检索必要的数据。为了避免内存泄漏,我在 useEffect 中有回调函数,每当我卸载组件时,该函数都会中止我的调用,但这不起作用。如果我在 API 调用完成之前进入组件并离开屏幕,React 的消息 警告:无法在未安装的组件上执行 React 状态更新。 仍然会出现。我不知道我错在哪里,所以我需要一些帮助。

提前致谢。

Currently I have defined in a functional component a useEffect as below

useEffect(() => {
 (async function () {
   posts.current = await BlogConsumer.getBlogPosts();
   setLoading(false);
 })();

 return () => {
   BlogConsumer.call_controller.abort();
 };
}, []);

where this BlogConsumer is defined as below

class BlogConsumer {
  static posts = {};
  static call_controller = new AbortController();

  static async getBlogPosts() {
    await axios
      .get('https://nice.api', {
        signal: this.call_controller.signal,
      })
      .then(response => {
        // treatment for success
      })
      .catch(error => {
        // treatment for erros
      });

    return this.posts;
  }
}

export default BlogConsumer;

The overral ideia is that in the render of the component I'll be calling a static method from my consumer and will retrieve the necessary data. For the pourpuse of not having memory leaks, I have my callback function in my useEffect that will abort my call whenever I unmount the component, but this is not working. React's message of Warning: Can't perform a React state update on an unmounted component. still appears if I enter the component and leave the screen before the API call is finished. I don't know where I am wrong, so I'd like a little help.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-01-18 09:03:51

您可以取消卸载请求。像这样:

export const fetchData = async (signal) => {
  try {
    const res = await instance.get("/pages/home", {
      signal,
    });
    return res.data;
  } catch (error) {
    return Promise.reject(error);
  }
};

useEffect(() => {
  const controller = new AbortController();
  fetchData(controller.signal);
  return () => {
    controller.abort()
  };
}, []);

You could just cancel the request on unmount. Like this:

export const fetchData = async (signal) => {
  try {
    const res = await instance.get("/pages/home", {
      signal,
    });
    return res.data;
  } catch (error) {
    return Promise.reject(error);
  }
};

useEffect(() => {
  const controller = new AbortController();
  fetchData(controller.signal);
  return () => {
    controller.abort()
  };
}, []);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文