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: new cache entry check #8151

Merged
merged 2 commits into from
Oct 9, 2024
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
23 changes: 23 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4971,6 +4971,29 @@ describe('useQuery', () => {
expect(renders).toBe(hashes)
})

it('should hash query keys that contain bigints given a supported query hash function', async () => {
const key = [queryKey(), 1n]

function queryKeyHashFn(x: any) {
return JSON.stringify(x, (_, value) => {
if (typeof value === 'bigint') return value.toString()
return value
})
}

function Page() {
useQuery({ queryKey: key, queryFn: () => 'test', queryKeyHashFn })
return null
}

renderWithClient(queryClient, <Page />)

await sleep(10)

const query = queryClient.getQueryCache().get(queryKeyHashFn(key))
expect(query?.state.data).toBe('test')
})

it('should refetch when changed enabled to true in error state', async () => {
const queryFn = vi.fn<(...args: Array<unknown>) => unknown>()
queryFn.mockImplementation(async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export function useBaseQuery<
useClearResetErrorBoundary(errorResetBoundary)

// this needs to be invoked before creating the Observer because that can create a cache entry
const isNewCacheEntry = !client.getQueryState(options.queryKey)
const isNewCacheEntry = !client
.getQueryCache()
.get(defaultedOptions.queryHash)

const [observer] = React.useState(
() =>
Expand Down
Loading