Skip to content

Commit 4d571d1

Browse files
authored
fix: Skip queryClient context lookup if client passed directly (#5669)
1 parent 268fb87 commit 4d571d1

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

packages/solid-query/src/QueryClientProvider.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ export const QueryClientContext = createContext<QueryClient | undefined>(
77
)
88

99
export const useQueryClient = (queryClient?: QueryClient) => {
10-
const client = useContext(QueryClientContext)
11-
1210
if (queryClient) {
1311
return queryClient
1412
}
13+
const client = useContext(QueryClientContext)
1514

1615
if (!client) {
1716
throw new Error('No QueryClient set, use QueryClientProvider to set one')

packages/solid-query/src/__tests__/QueryClientProvider.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,21 @@ describe('QueryClientProvider', () => {
161161
consoleMock.mockRestore()
162162
})
163163
})
164+
165+
it('should not throw an error if user provides custom query client', () => {
166+
const consoleMock = vi
167+
.spyOn(console, 'error')
168+
.mockImplementation(() => undefined)
169+
170+
function Page() {
171+
const client = createQueryClient()
172+
useQueryClient(client)
173+
return null
174+
}
175+
176+
render(() => <Page />)
177+
expect(consoleMock).not.toHaveBeenCalled()
178+
179+
consoleMock.mockRestore()
180+
})
164181
})

packages/svelte-query/src/useQueryClient.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import type { QueryClient } from '@tanstack/query-core'
22
import { getQueryClientContext } from './context'
33

44
export function useQueryClient(queryClient?: QueryClient): QueryClient {
5-
const client = getQueryClientContext()
6-
7-
if (queryClient) {
8-
return queryClient
9-
}
10-
11-
return client
5+
if (queryClient) return queryClient
6+
return getQueryClientContext()
127
}

0 commit comments

Comments
 (0)