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 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 *
23 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
24 * be used.
25 * @returns The current `QueryClient` instance.
26 * @throws If no `queryClient` argument is passed and no `QueryClientProvider` is found in the 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()
39}
40β
41export const useQueryClientResolver = (
42 queryClient?: Accessor<QueryClient | undefined>,
43): Accessor<QueryClient> => {
44 const contextClient = useContext(QueryClientContext)
45β
46 return () => {
47 const resolvedClient = queryClient?.()
48 if (resolvedClient) {
49 return resolvedClient
50 }
51β
52 if (!contextClient) {
53 throw new Error(queryClientContextError)
54 }
55β
56 return contextClient()
57 }
58}
59β
60/**
61 * The props accepted by `QueryClientProvider`.
62 */
63export type QueryClientProviderProps = {
64 /**
65 * **Required**
66 *
67 * The `QueryClient` instance to provide.
68 */
69 client: QueryClient
70 /**
71 * The components that get access to the provided `QueryClient`.
72 */
73 children?: JSX.Element
74}
75β
76/**
77 * Use the `QueryClientProvider` component to connect and provide a `QueryClient` to your application. Also
78 * calls `client.mount()`/`client.unmount()` as this component mounts/unmounts, which subscribes the client to
79 * focus/online events (resuming any paused mutations and refetching as needed when the app regains focus or
80 * comes back online).
81 *
82 * @returns The provided `children`, wrapped so they can read the `QueryClient` via `useQueryClient`.
83 *
84 * @example
85 * ```tsx
86 * import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
87 *
88 * const queryClient = new QueryClient()
89 *
90 * function App() {
91 * return <QueryClientProvider client={queryClient}>...</QueryClientProvider>
92 * }
93 * ```
94 */
95export const QueryClientProvider = (
96 props: QueryClientProviderProps,
97): JSX.Element => {
98 createRenderEffect<() => void>((unmount) => {
99 unmount?.()
100 props.client.mount()
101 return props.client.unmount.bind(props.client)
102 })
103 onCleanup(() => props.client.unmount())
104β
105 return (
106 <QueryClientContext.Provider value={() => props.client}>
107 {props.children}
108 </QueryClientContext.Provider>
109 )
110}