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