Make one configured QueryClient available throughout the app.
Application bootstrap
TanStack · Component
QueryClientProvider supplies a QueryClient to a React component tree. It also manages the client's mount and unmount lifecycle.
QueryClientProvider places the supplied QueryClient into React context so descendant components can retrieve it with useQueryClient. Mounting and unmounting the provider also mounts or unmounts the client, connecting it to focus and online events.
function QueryClientProvider(props: QueryClientProviderProps): Element;| Name | Type | Description |
|---|---|---|
client | QueryClient | The QueryClient instance to provide. |
children | React.ReactNode | The components that get access to the provided QueryClient. |
context | React.Context<QueryClient | undefined> | A custom React Query context; otherwise the default context is used. |
An Element containing the supplied children, with access to the provided QueryClient through context.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
const queryClient = new QueryClient()
export function App() {
return (
<QueryClientProvider client={queryClient}>
<Dashboard />
</QueryClientProvider>
)
}
function Dashboard() {
return <main>Queries can access the client</main>
}Creates one client and makes it available to the descendant component tree.
Application bootstrap
Query hooks
Focus and reconnect behavior
QueryClientProvider supplies a QueryClient to descendant components through context. Those components can then retrieve the client with useQueryClient.
Follow-up: How does the provider affect the client's lifecycle? It calls client.mount() when mounted and client.unmount() when removed, enabling focus and online event subscriptions.
The required client prop is the QueryClient instance that the provider exposes. The children receive access to that instance through the provider.
Follow-up: What happens if a component is outside the provider? It cannot obtain the provider's QueryClient through the normal context-based useQueryClient mechanism.
The React signature is function QueryClientProvider(props: QueryClientProviderProps): Element. Its props include a required client and optional children.
Follow-up: When might a custom context be relevant? The context prop lets an application use a custom React Query context instead of the default context.
QueryClientuseQueryClientQueryClientProviderProps