Skip to content

Commit

Permalink
context and contextSharing are mutually exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Galloway committed Feb 7, 2022
1 parent 9dfe188 commit 358dfd0
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/reactjs/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,41 @@ export const useQueryClient = ({ context }: ContextOptions = {}) => {
return queryClient
}

export interface QueryClientProviderProps extends ContextOptions {
type QueryClientProviderPropsBase = {
client: QueryClient
}
type QueryClientProviderPropsWithContext = ContextOptions &
QueryClientProviderPropsBase
type QueryClientProviderPropsWithContextSharing = {
contextSharing?: boolean
} & QueryClientProviderPropsBase

export type QueryClientProviderProps =
| QueryClientProviderPropsWithContext
| QueryClientProviderPropsWithContextSharing

const propsHasContext = (
props: QueryClientProviderProps
): props is QueryClientProviderPropsWithContext => {
return (props as QueryClientProviderPropsWithContext).context !== undefined
}

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

const Context = getQueryClientContext(context, contextSharing)
const contextSharing = propsHasContext(props)
? false
: props.contextSharing || false

const Context = propsHasContext(props)
? getQueryClientContext(props.context, false)
: getQueryClientContext(undefined, contextSharing)

return (
<QueryClientSharingContext.Provider value={contextSharing}>
Expand Down

0 comments on commit 358dfd0

Please sign in to comment.