Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTKQ useQuery hook does not refetch data after resetApiState() call #3119

Closed
emcol opened this issue Jan 25, 2023 · 8 comments · Fixed by #3333
Closed

RTKQ useQuery hook does not refetch data after resetApiState() call #3119

emcol opened this issue Jan 25, 2023 · 8 comments · Fixed by #3333
Milestone

Comments

@emcol
Copy link

emcol commented Jan 25, 2023

The issue is likely a bug in RTKQ and should have been fixed by #1735, although it does not seem so.

I provide a minimal example that showcase the issue in isolation and I share the codebase here

Below the steps to riproduce the bug:

  • Run dev server;
  • Notice that data is properly fetched;
  • Click on reset button;

Expected results:

Data is correctly re-fetched after the API reset, the useQuery first returns a loading status and then fulfills causing the component to be re-rendered since it is subscribed to the endpoint.

Actual result:

The page gets stuck in a data loading state and useQuery hook does not fulfill blocking the user flow.

@emcol emcol changed the title RTKQ useQuery hook don't refetch data after resetApiState() call RTKQ useQuery hook does not refetch data after resetApiState() call Jan 25, 2023
@emcol emcol changed the title RTKQ useQuery hook does not refetch data after resetApiState() call RTKQ useQuery hook does not refetch data after resetApiState() call Feb 13, 2023
@gimi-anders
Copy link

I have also noticed this issue, and as a workaround I got it to work using useLazyQuery instead of useQuery.

@trvbr
Copy link

trvbr commented Apr 6, 2023

The useLazyQuery hack worked for me, too. However, it would have involved a lot of code changes to update every request in my app, so I took a different approach that might work for others who are running into this bug.

First, I added a common tag to each endpoint. I used Main in this example, but you can use anything.

export const exampleApi = createApi({
  // (...other API options here...)
  tagTypes: ['Main'],
  endpoints: (builder) => ({
    getThing: builder.query({
      query: (id) => `things/${id}`,
      providesTags: ['Main'],
    }),
    // (...other API endpoints here...)
  })
})

Then, I used invalidateTags() like this:

dispatch(exampleApi.util.invalidateTags(['Main']))

@markerikson markerikson added this to the 1.9.x milestone Apr 6, 2023
@phryneas
Copy link
Member

phryneas commented Apr 6, 2023

Unfortunately we didn't get to this in time (really sorry!) and the reproduction is gone. Could you please add another reproduction?

@emcol
Copy link
Author

emcol commented Apr 6, 2023

Unfortunately we didn't get to this in time (really sorry!) and the reproduction is gone. Could you please add another reproduction?

Thank you very much for replying, the preview link should be updated now.

@emcol
Copy link
Author

emcol commented Apr 6, 2023

The useLazyQuery hack worked for me, too. However, it would have involved a lot of code changes to update every request in my app, so I took a different approach that might work for others who are running into this bug.

First, I added a common tag to each endpoint. I used Main in this example, but you can use anything.

export const exampleApi = createApi({
  // (...other API options here...)
  tagTypes: ['Main'],
  endpoints: (builder) => ({
    getThing: builder.query({
      query: (id) => `things/${id}`,
      providesTags: ['Main'],
    }),
    // (...other API endpoints here...)
  })
})

Then, I used invalidateTags() like this:

dispatch(exampleApi.util.invalidateTags(['Main']))

Thanks for the reply. I have already implemented a workaround like this but I didn't expect it to be necessary.

I expected the application to work even if the slice is reset and the component to request the data again, since they are necessary for rendering. That's why I opened an issue here.

@phryneas
Copy link
Member

phryneas commented Apr 7, 2023

After some investigation, I think this is because of our new batchActions approach - it seems we forgot to reset this internal state on the resetApiState action.

@phryneas
Copy link
Member

phryneas commented Apr 7, 2023

Could you please test the CI build from #3333? I just tested it in the repro and to me it seems to fix the problem.

@emcol
Copy link
Author

emcol commented Apr 7, 2023

I've just tested, I confirm that #3333 solves this issue. I have also updated again my preview app and seems to work, thank you again! 🤗

@emcol emcol closed this as completed Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants