Capítulo 73 de 80

Chapter 73: useQueryClient

Core Idea

Retrieves the QueryClient from the nearest QueryClientProvider — the standard way to reach the client imperatively inside a component (for invalidateQueries, setQueryData, etc.), as opposed to declaratively via useQuery.

Key Concepts

  • No required arguments: useQueryClient(queryClient?) — the optional argument overrides which client to return, otherwise resolves from context.

Code Examples

const queryClient = useQueryClient()
queryClient.invalidateQueries({ queryKey: ['todos'] })

Key Takeaways

  1. This is the hook used throughout the Mutations/Invalidation guides to reach queryClient from inside a component — pair it with any imperative QueryClient method.

Connects To

  • QueryClientProvider: the context this hook reads from.
  • QueryClient: the full method surface this hook grants access to.