Skip to content

Commit fc14250

Browse files
authored
fix (QueryClient): disable context sharing per default (#1912)
* fix (QueryClient): disable context sharing per default but still allow for it by adding a `contextSharing` boolean prop when creating the QueryClient context * document the contextSharing prop
1 parent 401e188 commit fc14250

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

docs/src/pages/reference/QueryClientProvider.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ function App() {
1414
return <QueryClientProvider client={queryClient}>...</QueryClientProvider>
1515
}
1616
```
17+
**Options**
18+
19+
- `client: QueryClient`
20+
- **Required**
21+
- the QueryClient instance to provide
22+
- `contextSharing: boolean`
23+
- defaults to `false`
24+
- Set this to `true` to enable context sharing, which will share the first and at least one instance of the context across the window to ensure that if React Query is used across different bundles or microfrontends they will all use the same **instance** of context, regardless of module scoping.

src/react/QueryClientProvider.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ declare global {
99
}
1010

1111
const defaultContext = React.createContext<QueryClient | undefined>(undefined)
12+
const QueryClientSharingContext = React.createContext<boolean>(false)
1213

13-
// We share the first and at least one
14+
// if contextSharing is on, we share the first and at least one
1415
// instance of the context across the window
1516
// to ensure that if React Query is used across
1617
// different bundles or microfrontends they will
1718
// all use the same **instance** of context, regardless
1819
// of module scoping.
19-
function getQueryClientContext() {
20-
// @ts-ignore (for global)
21-
if (typeof window !== 'undefined') {
20+
function getQueryClientContext(contextSharing: boolean) {
21+
if (contextSharing && typeof window !== 'undefined') {
2222
if (!window.ReactQueryClientContext) {
2323
window.ReactQueryClientContext = defaultContext
2424
}
@@ -30,7 +30,9 @@ function getQueryClientContext() {
3030
}
3131

3232
export const useQueryClient = () => {
33-
const queryClient = React.useContext(getQueryClientContext())
33+
const queryClient = React.useContext(
34+
getQueryClientContext(React.useContext(QueryClientSharingContext))
35+
)
3436

3537
if (!queryClient) {
3638
throw new Error('No QueryClient set, use QueryClientProvider to set one')
@@ -41,10 +43,12 @@ export const useQueryClient = () => {
4143

4244
export interface QueryClientProviderProps {
4345
client: QueryClient
46+
contextSharing?: boolean
4447
}
4548

4649
export const QueryClientProvider: React.FC<QueryClientProviderProps> = ({
4750
client,
51+
contextSharing = false,
4852
children,
4953
}) => {
5054
React.useEffect(() => {
@@ -54,7 +58,11 @@ export const QueryClientProvider: React.FC<QueryClientProviderProps> = ({
5458
}
5559
}, [client])
5660

57-
const Context = getQueryClientContext()
61+
const Context = getQueryClientContext(contextSharing)
5862

59-
return <Context.Provider value={client}>{children}</Context.Provider>
63+
return (
64+
<QueryClientSharingContext.Provider value={contextSharing}>
65+
<Context.Provider value={client}>{children}</Context.Provider>
66+
</QueryClientSharingContext.Provider>
67+
)
6068
}

0 commit comments

Comments
 (0)