QueryClientProvider
TheQueryClientProvider component provides the QueryClient instance to your React application. It must wrap any components that use TanStack Query hooks.
Import
Signature
Props
QueryClient
required
The QueryClient instance to provide to the application. Create this using
new QueryClient().React.ReactNode
The React components that will have access to the QueryClient.
Behavior
TheQueryClientProvider component:
- Creates a React Context to provide the QueryClient
- Calls
client.mount()when the component mounts - Calls
client.unmount()when the component unmounts - Makes the QueryClient available to all child components via the
useQueryClienthook
Examples
Basic Setup
With Custom Configuration
With React DevTools
Multiple Clients (Advanced)
With Persistence (Server-Side Rendering)
Accessing the Client in Components
Related Hooks
useQueryClient
TheuseQueryClient hook allows you to access the QueryClient instance from any component within the provider:
Implementation Details
The implementation creates a React Context and manages the QueryClient lifecycle:Common Patterns
Single Client Instance
Per-Request Client (SSR)
With Error Handling
Best Practices
- Create client outside component: Create the QueryClient instance outside your component or use
useStateto ensure it’s only created once - Don’t create new clients on every render: This will cause all queries to reset
- Place high in component tree: Wrap your entire app or the highest level component that needs TanStack Query
- Use single client for most apps: Most applications only need one QueryClient instance
- SSR considerations: Create a new client per request in SSR environments to avoid sharing data between users
- DevTools in development: Include ReactQueryDevtools only in development builds