Skip to content

Commit

Permalink
Restore QueryClientProviderPropsWithContext and QueryClientProviderPr…
Browse files Browse the repository at this point in the history
…opsWithContextSharing
  • Loading branch information
Nick Galloway committed Mar 20, 2022
1 parent 0794e1f commit 9cb33fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
9 changes: 5 additions & 4 deletions docs/src/pages/reference/QueryClientProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function App() {
- `client: QueryClient`
- **Required**
- the QueryClient instance to provide
- `context?: React.Context<QueryClient | undefined> | 'sharing'`
- Set this to React Context of type `QueryClient | undefined` to use a custom context for this provider.
- Set this to `sharing` 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.
- Otherwise, this defaults to `undefined`, in which the default context will be used.
- `contextSharing: boolean`
- defaults to `false`
- 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.
- `context?: React.Context<QueryClient | undefined>`
- Use this to use a custom React Query context. Otherwise, the default will be used.
29 changes: 20 additions & 9 deletions src/reactjs/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,38 @@ export const useQueryClient = ({ context }: ContextOptions = {}) => {
return queryClient
}

export interface QueryClientProviderProps {
type QueryClientProviderPropsBase = {
client: QueryClient
context?: React.Context<QueryClient | undefined> | 'sharing'
}
type QueryClientProviderPropsWithContext = ContextOptions & {
contextSharing?: never
} & QueryClientProviderPropsBase
type QueryClientProviderPropsWithContextSharing = {
context?: never
contextSharing?: boolean
} & QueryClientProviderPropsBase

export const QueryClientProvider: React.FC<QueryClientProviderProps> = props => {
const { client, children } = props
export type QueryClientProviderProps =
| QueryClientProviderPropsWithContext
| QueryClientProviderPropsWithContextSharing

export const QueryClientProvider: React.FC<QueryClientProviderProps> = ({
client,
children,
context,
contextSharing = false,
}) => {
React.useEffect(() => {
client.mount()
return () => {
client.unmount()
}
}, [client])

const propsContext = props.context === 'sharing' ? undefined : props.context
const contextSharing = props.context === 'sharing'

const Context = getQueryClientContext(propsContext, contextSharing)
const Context = getQueryClientContext(context, contextSharing)

return (
<QueryClientSharingContext.Provider value={contextSharing}>
<QueryClientSharingContext.Provider value={!context && contextSharing}>
<Context.Provider value={client}>{children}</Context.Provider>
</QueryClientSharingContext.Provider>
)
Expand Down
16 changes: 11 additions & 5 deletions src/reactjs/tests/QueryClientProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,16 @@ describe('QueryClientProvider', () => {
)
}

// contextSharing should be ignored when passing a custom context.
const contextSharing = true

const rendered = render(
<QueryClientProvider client={queryClientOuter} context={contextOuter}>
<QueryClientProvider client={queryClientInner} context={contextInner}>
<QueryClientProvider client={queryClientInnerInner}>
<QueryClientProvider
client={queryClientInnerInner}
contextSharing={contextSharing}
>
<Page />
</QueryClientProvider>
</QueryClientProvider>
Expand Down Expand Up @@ -212,7 +218,7 @@ describe('QueryClientProvider', () => {
consoleMock.mockRestore()
})

test('should use window to get the context when context sharing', () => {
test('should use window to get the context when contextSharing is true', () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

Expand All @@ -230,7 +236,7 @@ describe('QueryClientProvider', () => {
}

render(
<QueryClientProvider client={queryClient} context={'sharing'}>
<QueryClientProvider client={queryClient} contextSharing={true}>
<Page />
</QueryClientProvider>
)
Expand All @@ -239,7 +245,7 @@ describe('QueryClientProvider', () => {
expect(queryClientFromWindow).toEqual(queryClient)
})

test('should not use window to get the context when context sharing and window does not exist', () => {
test('should not use window to get the context when contextSharing is true and window does not exist', () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

Expand All @@ -256,7 +262,7 @@ describe('QueryClientProvider', () => {
}

renderToString(
<QueryClientProvider client={queryClient} context={'sharing'}>
<QueryClientProvider client={queryClient} contextSharing={true}>
<Page />
</QueryClientProvider>
)
Expand Down

0 comments on commit 9cb33fa

Please sign in to comment.