-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathQueryClientProvider.tsx
More file actions
38 lines (31 loc) · 890 Bytes
/
QueryClientProvider.tsx
File metadata and controls
38 lines (31 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createContext, onCleanup, useContext } from 'solid-js'
import type { QueryClient } from './QueryClient'
import type { JSX } from '@solidjs/web'
export const QueryClientContext = createContext<(() => QueryClient) | null>(
null,
)
export const useQueryClient = (queryClient?: QueryClient) => {
if (queryClient) {
return queryClient
}
const client = useContext(QueryClientContext)
if (!client) {
throw new Error('No QueryClient set, use QueryClientProvider to set one')
}
return client()
}
export type QueryClientProviderProps = {
client: QueryClient
children?: JSX.Element
}
export const QueryClientProvider = (
props: QueryClientProviderProps,
): JSX.Element => {
props.client.mount()
onCleanup(() => props.client.unmount())
return (
<QueryClientContext value={() => props.client}>
{props.children}
</QueryClientContext>
)
}