File tree Expand file tree Collapse file tree 1 file changed +23
-9
lines changed Expand file tree Collapse file tree 1 file changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -2,15 +2,31 @@ import React from 'react'
2
2
3
3
import { QueryClient } from '../core'
4
4
5
- const QueryClientContext = React . createContext < QueryClient | undefined > (
6
- undefined
7
- )
5
+ const QueryClientContext = ( ( ) => {
6
+ const context = React . createContext < QueryClient | undefined > ( undefined )
7
+ if ( typeof window !== 'undefined' ) {
8
+ // @ts -ignore
9
+ window . ReactQueryClientContext = context
10
+ }
11
+ return context
12
+ } ) ( )
13
+
14
+ function getQueryClientContext ( ) {
15
+ return typeof window !== 'undefined'
16
+ ? // @ts -ignore
17
+ ( window . ReactQueryClientContext as React . Context <
18
+ QueryClient | undefined
19
+ > ) ?? QueryClientContext
20
+ : QueryClientContext
21
+ }
8
22
9
23
export const useQueryClient = ( ) => {
10
- const queryClient = React . useContext ( QueryClientContext )
24
+ const queryClient = React . useContext ( getQueryClientContext ( ) )
25
+
11
26
if ( ! queryClient ) {
12
27
throw new Error ( 'No QueryClient set, use QueryClientProvider to set one' )
13
28
}
29
+
14
30
return queryClient
15
31
}
16
32
@@ -29,9 +45,7 @@ export const QueryClientProvider: React.FC<QueryClientProviderProps> = ({
29
45
}
30
46
} , [ client ] )
31
47
32
- return (
33
- < QueryClientContext . Provider value = { client } >
34
- { children }
35
- </ QueryClientContext . Provider >
36
- )
48
+ const Context = getQueryClientContext ( )
49
+
50
+ return < Context . Provider value = { client } > { children } </ Context . Provider >
37
51
}
You can’t perform that action at this time.
0 commit comments