Invalidating a query key based on 2nd 3rd etc element in the query key array #7263
Replies: 3 comments
-
not like that, no. You would need to have objects, because they have no order:
then use:
or, you nest them like this:
|
Beta Was this translation helpful? Give feedback.
-
I found this answer because I couldn't find the documented behavior of invalidateQueries when using object as part of the key. The page for https://tanstack.com/query/v5/docs/framework/react/guides/query-invalidation doesn't mention how object keys are filtered when only one of the properties is provided. Most of the examples are more about arrays (which I get is the most common use case). Your example on this answer is the first result I could find that mentions this behavior (filtering by partial object). Is there such documentation somewhere? (I can't change the way my keys are structured. The project uses https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-react-query. I like the current behavior, I just wanted to confirm it was correct, and couldn't find the docs) |
Beta Was this translation helpful? Give feedback.
-
I had this same requirement, I thought queries do a match within whole of the array by default and then figured out its not. I got it working like this though, preserving the existing query key structure. queryClient.invalidateQueries({
predicate: (query) => query.queryKey.includes(1234)
}) cc @TkDodo wonder if it makes sense adding this use case in the docs |
Beta Was this translation helpful? Give feedback.
-
Let's say you have a site with query keys like this:
Is it possible to invalidate just
1234
so that both queries reload?Beta Was this translation helpful? Give feedback.
All reactions