Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

QueryClientProvider.tsx

115 lines3.3 KB

1import {
2 createContext,
3 createRenderEffect,
4 onCleanup,
5 useContext,
6} from 'solid-js'
7import type { Accessor, JSX } from 'solid-js'
8import type { QueryClient } from './QueryClient'
9​
10/**
11 * The context that `useQueryClient` reads from. `QueryClientProvider` is the normal way to set it.
12 */
13export const QueryClientContext = createContext<
14 (() => QueryClient) | undefined
15>(undefined)
16​
17const queryClientContextError =
18 'No QueryClient set, use QueryClientProvider to set one'
19​
20/**
21 * The `useQueryClient` primitive returns the current `QueryClient` instance.
22 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
23 * be used.
24 * @returns The current `QueryClient` instance.
25 * @throws {Error} If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
26 * component tree.
27 */
28export const useQueryClient = (queryClient?: QueryClient) => {
29 if (queryClient) {
30 return queryClient
31 }
32 const client = useContext(QueryClientContext)
33​
34 if (!client) {
35 throw new Error(queryClientContextError)
36 }
37​
38 return client()
40​
41/**
42 * Resolves the `QueryClient` to use: the given one, or else the one from the nearest
43 * `QueryClientProvider`.
44 * @param queryClient - Returns a custom `QueryClient`, if any.
45 * @returns An accessor that returns the resolved client. It throws if neither client is available.
46 */
47export const useQueryClientResolver = (
48 queryClient?: Accessor<QueryClient | undefined>,
49): Accessor<QueryClient> => {
50 const contextClient = useContext(QueryClientContext)
51​
52 return () => {
53 const resolvedClient = queryClient?.()
54 if (resolvedClient) {
55 return resolvedClient
56 }
57​
58 if (!contextClient) {
59 throw new Error(queryClientContextError)
60 }
61​
62 return contextClient()
63 }
65​
66/**
67 * The props accepted by `QueryClientProvider`.
68 */
69export type QueryClientProviderProps = {
70 /**
71 * **Required**
72 *
73 * The `QueryClient` instance to provide.
74 */
75 client: QueryClient
76 /**
77 * The components that get access to the provided `QueryClient`.
78 */
79 children?: JSX.Element
81​
82/**
83 * Use the `QueryClientProvider` component to connect and provide a `QueryClient` to your application. Also
84 * calls `client.mount()`/`client.unmount()` as this component mounts/unmounts, which subscribes the client to
85 * focus/online events (resuming any paused mutations and refetching as needed when the app regains focus or
86 * comes back online).
87 * @param props - The `client` to provide, and the `children` that get access to it.
88 * @returns The provided `children`, wrapped so they can read the `QueryClient` via `useQueryClient`.
89 * @example
90 * ```tsx
91 * import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
92 *
93 * const queryClient = new QueryClient()
94 *
95 * function App() {
96 * return <QueryClientProvider client={queryClient}>...</QueryClientProvider>
97 * }
98 * ```
99 */
100export const QueryClientProvider = (
101 props: QueryClientProviderProps,
102): JSX.Element => {
103 createRenderEffect<() => void>((unmount) => {
104 unmount?.()
105 props.client.mount()
106 return props.client.unmount.bind(props.client)
107 })
108 onCleanup(() => props.client.unmount())
109​
110 return (
111 <QueryClientContext.Provider value={() => props.client}>
112 {props.children}
113 </QueryClientContext.Provider>
114 )