Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Skip queryClient context lookup when client is passed directly #5669

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}