Skip to content

Commit

Permalink
fix: Skip queryClient context lookup if client passed directly (#5669)
Browse files Browse the repository at this point in the history
  • Loading branch information
skokenes authored Jul 3, 2023
1 parent 268fb87 commit 4d571d1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 1 addition & 2 deletions packages/solid-query/src/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ export const QueryClientContext = createContext<QueryClient | undefined>(
)

export const useQueryClient = (queryClient?: QueryClient) => {
const client = useContext(QueryClientContext)

if (queryClient) {
return queryClient
}
const client = useContext(QueryClientContext)

if (!client) {
throw new Error('No QueryClient set, use QueryClientProvider to set one')
Expand Down
17 changes: 17 additions & 0 deletions packages/solid-query/src/__tests__/QueryClientProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,21 @@ describe('QueryClientProvider', () => {
consoleMock.mockRestore()
})
})

it('should not throw an error if user provides custom query client', () => {
const consoleMock = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)

function Page() {
const client = createQueryClient()
useQueryClient(client)
return null
}

render(() => <Page />)
expect(consoleMock).not.toHaveBeenCalled()

consoleMock.mockRestore()
})
})
9 changes: 2 additions & 7 deletions packages/svelte-query/src/useQueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import type { QueryClient } from '@tanstack/query-core'
import { getQueryClientContext } from './context'

export function useQueryClient(queryClient?: QueryClient): QueryClient {
const client = getQueryClientContext()

if (queryClient) {
return queryClient
}

return client
if (queryClient) return queryClient
return getQueryClientContext()
}

0 comments on commit 4d571d1

Please sign in to comment.