最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How to handle for a request that takes to long to download content using useQuery from React Query? - Stack Overflow

programmeradmin5浏览0评论

I'm using useQuery function from React Query to fetch data in my React application. However, I'm encountering an issue where some requests take too long to download content (about a minute).

Here's a is my code:

const useServiceHook = ({
  columns,
  currentPage,
  title,
  enabled,
  filters,
  pageSize,
  sorting,
}) => {
  const { data, isLoading, status } = useQuery({
    enabled,
    refetchOnWindowFocus: false,
    queryKey: [
      'table-listing',
      columns,
      currentPage,
      title,
      filters,
      pageSize,
      sorting,
    ],
    select: ({ data }) => data,
    queryFn: () =>
      API.post(
        generatePath(URL, { title }),
        {
          columns,
          filters: [{ and: filters }],
        },
        {
          params: {
            page: currentPage,
            size: pageSize,
            sort: sorting,
          },
        },
      ).catch(() => ({
        content: [],
        total_elements: 0,
        total_pages: 0,
      })),
    onSuccess: (data) => {
      console.log('log sucess', data);
    },
  });

  const rows = useMemo(() => (data ? data.content : []), [data]);

  return {
    rows,
    totalNumberOfPages: data ? data.total_pages : 0,
    totalNumberOfElements: data ? data.total_elements : 0,
    isLoading,
  };
};

export { useServiceHook };

API response Image

When I run this code, I receive from the useQuery that the request is successful, but I don't have any data.

There's something that I can do to receive the data from the API ?

I'm using useQuery function from React Query to fetch data in my React application. However, I'm encountering an issue where some requests take too long to download content (about a minute).

Here's a is my code:

const useServiceHook = ({
  columns,
  currentPage,
  title,
  enabled,
  filters,
  pageSize,
  sorting,
}) => {
  const { data, isLoading, status } = useQuery({
    enabled,
    refetchOnWindowFocus: false,
    queryKey: [
      'table-listing',
      columns,
      currentPage,
      title,
      filters,
      pageSize,
      sorting,
    ],
    select: ({ data }) => data,
    queryFn: () =>
      API.post(
        generatePath(URL, { title }),
        {
          columns,
          filters: [{ and: filters }],
        },
        {
          params: {
            page: currentPage,
            size: pageSize,
            sort: sorting,
          },
        },
      ).catch(() => ({
        content: [],
        total_elements: 0,
        total_pages: 0,
      })),
    onSuccess: (data) => {
      console.log('log sucess', data);
    },
  });

  const rows = useMemo(() => (data ? data.content : []), [data]);

  return {
    rows,
    totalNumberOfPages: data ? data.total_pages : 0,
    totalNumberOfElements: data ? data.total_elements : 0,
    isLoading,
  };
};

export { useServiceHook };

API response Image

When I run this code, I receive from the useQuery that the request is successful, but I don't have any data.

There's something that I can do to receive the data from the API ?

Share Improve this question edited Mar 11 at 7:37 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Mar 10 at 19:45 SeuZeRicardoSeuZeRicardo 156 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

you are waiting 60 seconds for your content? I would say the problem is in your api instead of frontend, can you in anyway batch your requests, so you are sending more requests in order to receive smaller responses?

UPDATED: The problem shouldnt be in react query. maybe you can swap your post to axios, because it has a timeout property, and dont fet to disable Retry in react-query,. if you only want to do this once.

 const { data } = useQuery(
    [key],
    // Use timeout you need
    () => axios.get(url, { timeout: 6000 }),
  );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论