Replies: 6 comments 16 replies
-
The easiest way to observe a query is by far |
Beta Was this translation helpful? Give feedback.
-
Hey guys, I am running into a similar situation as well. in my case, I have multiple queries with different keys based on some sort of UI filter, but I have a place in the app where I need to read all of the cache histories and merge into a big one. I used the useQuery with queryFn and it looks to work, but I feel this is not properly right. Is there a good way to achieve this? For more context, in the onSuccess hook, I am setting the single key cache to merge all of the data based on diff filter keys, and then the app reads this single to get all the history I mentioned above. Thanks. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo we have a use case where a parent component fires a list query e.g. |
Beta Was this translation helpful? Give feedback.
-
I see from the responses here that the maintainers are conflicted with the user requests. I want to observe a possible request that might or might not go out. And get it's data in a different part of the app. |
Beta Was this translation helpful? Give feedback.
-
I also need this kind of functionality. To give you another use case, let's say we have data for various users:
The app might invalidate either a single user's data:
...or all users' data:
I need an observer that would notify me about changes to data of any user. I tried QueryObserver:
But this doesn't work, QueryObserver complains about missing queryFn and no events are fired. Maybe QueryObserver is not the right tool for the job, but as far as I can tell, the only way of doing what I need, is to use |
Beta Was this translation helpful? Give feedback.
-
In my case I wanted to subscribe to data for a specific query (so I could put it in localstorage without messing around with the much more complex persisters https://tanstack.com/query/latest/docs/react/plugins/persistQueryClient) and ran into the same issue where The solution is similar to what @bladeSk posted above, and I'm thankful to him for demonstrating the paradigm let unsubscribe = queryClient.getQueryCache().subscribe((event) => {
if (
event.type === 'updated' &&
event.action.type === 'success' &&
matchQuery({queryKey: yourQueryKey}, event.query)
) {
console.log(event.action.data)
}
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to observe, from a component, data fetched and cached by another component, but I can't make it work.
Upon authentication, the
<Auth/>
component fetches the user profile and keeps it into a cache:A
<Profile/>
component is observing the cache and ready to display the profile userId:But it's not working:
No
result.data
is received by the'profile'
cache observer in the<Profile/>
component.The logs display an odd (unexpected ?) error message:
Error message:
And the data appears in the cache:
What am I missing ?
Env:
Beta Was this translation helpful? Give feedback.
All reactions