Invalidate query in react native
Using an infinite query in react native. One screen has
On another screen I allow the user to update their profile picture. After the image is uploaded I run
To invalidate all queries related to that user (i.e. posts, comments, etc that would all have the profile picture associated with them) . This however is not causing my list of posts to update when I return to the screen. Only after I refresh (which triggers a re-fetch manually) will it update the posts.
Why might this be?
5 Replies
exotic-emeraldOP•3y ago
@M00LTi would it matter if it was within a mutate or not?
wise-white•3y ago
Yes. You might want to share some code.
flat-fuchsia•3y ago
To invalidate all queries related to that userthat's not how it works. Query Keys hav a hierarchy - arrays have an order. if you want that, you'd need to put the id first in the key: alternatively, an object structure also works well: because with that, you can cross-invalidate: - everything post related with
[{ type: 'posts'}]
- everything for a single user with [{ user: user.uid }]
I have a blogpost on this: https://tkdodo.eu/blog/leveraging-the-query-function-context#object-query-keysLeveraging the Query Function Context
Use what React Query provides for optimal type safety
flat-fuchsia•3y ago
Only after I refreshinvalidate refetches active queries and marks inactive ones as stale so that they will be refetched once they are used again. You can use
refetchQueries instead to trigger a refetch immediately.