T
TanStack•3y ago
extended-salmon

InvalidateQueries of multiple keys

If I have a query such as this: const todoListQuery = useQuery({ queryKey: ['todosFull', 'todoSpecific'], queryFn: fetchTodoList, }) and then I invalidate the query with queryClient.invalidateQueries({ queryKey: ['todoSpecific'] }) Should that work? or do I need to do queryClient.invalidateQueries({ queryKey: ['todosFull', 'todoSpecific'] }) for it
4 Replies
environmental-rose
environmental-rose•3y ago
Yes that works Except if you pass the exact flag
exotic-emerald
exotic-emerald•3y ago
no, it wouldn't work. arrays have an order. The key ['todosFull', 'todoSpecific'] would be targeted by:
queryClient.invalidateQueries({ queryKey: ['todosFull'] })
queryClient.invalidateQueries({ queryKey: ['todosFull'] })
or, of course:
queryClient.invalidateQueries({ queryKey: ['todosFull', 'todoSpecific'] })
queryClient.invalidateQueries({ queryKey: ['todosFull', 'todoSpecific'] })
I have a blogpost on this: https://tkdodo.eu/blog/effective-react-query-keys
Effective React Query Keys
Learn how to structure React Query Keys effectively as your App grows
environmental-rose
environmental-rose•3y ago
Oops, did not read it right 🙃
extended-salmon
extended-salmonOP•3y ago
Always ends up with the solution in your blogposts, should be advertised more! Thanks again

Did you find this page helpful?