useQuery and useInfiniteQuery's return data gets mixed up #1255
-
Describe the bug To Reproduce
Expected behavior The return value of both queries should consistently be the data for I also noticed the docs https://react-query.tanstack.com/docs/api#useinfinitequery don't mention that the Desktop (please complete the following information):
Additional context We discovered this issue in production, it was pretty hard to reproduce as it required a specific flow of requests to happen. I think what we'll do for now is disable the caching on these specific queries until the bug is resolved. Thanks for your work on this great library! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
My mental model of react-query is the following: the react query cache is just an object, and the serialized query key is the key of that object. When you fetch data from the backend, you put data into that cache and then just retrieve it via useQuery with a lookup. React-query will refetch and update that cache. useQuery and useInfiniteQuery have different underlying data structures, so by using the same key, you are overriding the data. This is not a good idea - It’s like calling My proposed solution would be:
|
Beta Was this translation helpful? Give feedback.
-
same issue fixed for now by adding a single unique key to all useInfiniteQuery keys - [endpoint, query]
+ ['Infinite', endpoint, query] |
Beta Was this translation helpful? Give feedback.
-
I'm doing the same as @MohammedFaragallah. This was definitely not intuitive and caused a bug in blitz earlier. However, I'm not sure there's a good "fix". At a minimum, something that'd greatly help is if react-query can throw an error if it sees you are using both useQuery and useInfiniteQuery with the same query key. |
Beta Was this translation helpful? Give feedback.
-
I like this idea. |
Beta Was this translation helpful? Give feedback.
-
A PR to implement this warning is welcome :) |
Beta Was this translation helpful? Give feedback.
My mental model of react-query is the following: the react query cache is just an object, and the serialized query key is the key of that object. When you fetch data from the backend, you put data into that cache and then just retrieve it via useQuery with a lookup. React-query will refetch and update that cache.
useQuery and useInfiniteQuery have different underlying data structures, so by using the same key, you are overriding the data. This is not a good idea - It’s like calling
queryCache.setQueryData()
and putting data of different structure into your cache.My pro…