Skip to content
Source
Sign inSign up
TanStack/query

isLoading is always return true after go other page 2 too fast (before loading in page 1 success) #8281

Send to my agent
Closedducyb782001(GitHub profile, opens in a new tab) opened this issue 16 comments

Describe the bug

I have page 1 /photos and two useQuery function const { data: profileDetail } = useQuery({ queryKey: ["userProfile"], queryFn: () => getProfileDetail(), }); const { data: listUserToken, isLoading: isLoadingUserToken } = useQuery({ queryKey: ["listUserToken"], queryFn: async () => { const result = await getListUserToken(); return result; }, }); I hard reload this page and don't wait the query is success => and click to push in page 2

<div onClick={() => { router.push("/photos/1"); }} > Click {isLoadingUserToken ? "Loading..." : "Done"} </div>

In page 2 i will have some other useQuery function const { data: userExp } = useQuery({ queryKey: ["userExp"], queryFn: () => getUserExp(), }); const { data: profileDetail } = useQuery({ queryKey: ["userProfile"], queryFn: () => getProfileDetail(), }); const { data: userLevel, isLoading: isLoadingUserLevel } = useQuery({ queryKey: ["getUserLevel"], queryFn: () => getUserLevel(), });

And then i don't wait the query success, i click to push page into page 1 (/photos) <div onClick={() => { router.push("/photos"); }} > Back </div> {isLoadingUserLevel ? <div>Loading....</div> : "Loading done"}

And now, isLoadingUserLevel always return true

Your minimal, reproducible example

https://stackblitz.com/edit/stackblitz-starters-xmhdkt?file=app%2Fpage.tsx

Steps to reproduce

Step 1: Click go to photo Detail Step 2: Reload page Photo detail and don't wait api loading success Step 3: Spam click button go to list photo in page PhotoDetail (Dont wait api loading success) Step 4: When go to List photo, spam again on button go to photo detail

Expected behavior

I expected isLoading work after reload and go to other page fast

How often does this bug happen?

Always when using this step

Screenshots or Videos

https://github.com/user-attachments/assets/33bc371b-052c-4136-af00-5bec36063a03

Platform

  • OS: MacOS Monterey 12.7.6
  • Chrome: Version 130.0.6723.117 (Official Build) (x86_64)
  • NextJs: 14.2.13

Tanstack Query adapter

react-query

TanStack Query version

^4.35.3

TypeScript version

^5

Additional context

No response

  1. Thank you for filing this issue, but we need more information before looking into this.

    Specifically, a minimal, standalone, runnable reproduction is needed - preferably with codesandbox or stackblitz.

    We'll close this issue for now, but we'll certainly re-open it if there is a proper reproduction.

  2. TkDodo closed this

  3. TkDodo added labelsneeds-info

  4. @TkDodo thanks for your reply, i will give you a code sanbox soon. And you can see my video for more infor. Thanks

  5. Hi @TkDodo , Sorry for the slowly response, i already update the stackbliz code example and step to have the bug. Here is my stackbliz link

    https://stackblitz.com/edit/stackblitz-starters-xmhdkt?file=app%2Fpage.tsx

  6. TkDodo reopened this

  7. TkDodo removed labelsneeds-info

  8. It’s an interesting race condition. Here’s a fork with v5 + the devtools where it still shows up:

    https://stackblitz.com/edit/stackblitz-starters-fuohuu

    It goes away when the list query is not used on the details page. I’ll have to look into why that happens.

  9. Would be good to know if this is specific to nextJs or if it could be reproduced in other frameworks as well

  10. I happened in the "Link" component of nextjs. If I moved the page before useQuery was done, the same thing happened to me.

    https://stackblitz.com/edit/stackblitz-starters-4tjsxt?file=app%2Fpage.tsx Image

    It didn't happen because the page was reloaded when using the 'a' tag.

  11. @parkinjong so, to be on the same page: the issue doesn’t show up when router.push is used, but it does show up when a next Link is used ?

  12. @TkDodo When I tested it on stackblitz, router.push and Link look the same

  13. Does anyone have some suggested ideas for this issues? If the user presses too fast and the error does not load, it will reduce the user experience. And if i prevent user from click any thing after api succeed, it still not good.

  14. facing same issue, dont we have some fix ?

  15. Hi @TkDodo I see another problem in nextjs v13 (or higher) when call external api using use server and using useQuery. All the function is not called concurrently. I've linked it with a new discussion

  16. Been getting ths issue for the last 3 months, with next-link. This is a pretty bad bug sadly

  17. I have just seen in the reproduction that server actions (’use server’) is used to make API requests.

    I don’t know how often I have to say that server actions are for updating data and they are absolutely NOT meant for querying data.

    It seems like everybody is hellbent on not having to write API endpoints but nextJs server actions are NOT the way.

    I won’t look into this anymore until someone shows a repro of the bug WITHOUT server actions. I’ve wasted way too much time on this already.

  18. TkDodo closed this

  19. TkDodo added labelsneeds-info

  20. note: everything works without server actions:

    https://stackblitz.com/edit/stackblitz-starters-bswipehc?file=app%2Flayout.tsx,app%2Fphotos%2Fpage.tsx,app%2Fphotos%2F[id]%2Fpage.tsx,components%2FPhotoDetail.tsx,apis%2Fproduct-module.ts,apis%2Fclient-infra.ts,apis%2Faxios-instance.ts

    I’ve also added a simple log statement around the server action call:

      console.log('getAPI started');
      const result = await getAPIServerSide(initData, options);
      console.log('getAPI finished');
    
      return result;

    and in cases where the query is “stuck”, there is no getAPI finished log statement for the list call. I see getAPI started twice and getAPI finished once. So yeah, it is “stuck”, but not because react-query keeps it stuck, it’s because the server action never terminates / returns in these cases (for whatever reason)

Sign in to join this conversation.