Skip to content

Commit e8a22e9

Browse files
committed
fix: cross-bundle/version query client context
1 parent d773e97 commit e8a22e9

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/react/QueryClientProvider.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,31 @@ import React from 'react'
22

33
import { QueryClient } from '../core'
44

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+
}
822

923
export const useQueryClient = () => {
10-
const queryClient = React.useContext(QueryClientContext)
24+
const queryClient = React.useContext(getQueryClientContext())
25+
1126
if (!queryClient) {
1227
throw new Error('No QueryClient set, use QueryClientProvider to set one')
1328
}
29+
1430
return queryClient
1531
}
1632

@@ -29,9 +45,7 @@ export const QueryClientProvider: React.FC<QueryClientProviderProps> = ({
2945
}
3046
}, [client])
3147

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>
3751
}

0 commit comments

Comments
 (0)