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(react-query): do not overwrite staleTime with the suspense default value if it's a function #8174

Merged
merged 3 commits into from
Oct 13, 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
40 changes: 40 additions & 0 deletions packages/react-query/src/__tests__/suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,46 @@ describe('useSuspenseQuery', () => {
await waitFor(() => rendered.getByText('fetching: false'))
})

it('should set staleTime when having passed a function', async () => {
const key = queryKey()
let count = 0

function Component() {
const result = useSuspenseQuery({
queryKey: key,
queryFn: async () => {
await sleep(5)
count++
return count
},
staleTime: () => 60 * 1000,
})
return (
<div>
<span>data: {result.data}</span>
</div>
)
}

function Page() {
return (
<React.Suspense fallback="Loading...">
<Component />
</React.Suspense>
)
}

const rendered = renderWithClient(queryClient, <Page />)

await waitFor(() => rendered.getByText('Loading...'))
await waitFor(() => rendered.getByText('data: 1'))

expect(
typeof queryClient.getQueryCache().find({ queryKey: key })?.observers[0]
?.options.staleTime,
).toBe('function')
})

it('should suspend when switching to a new query', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down
2 changes: 1 addition & 1 deletion packages/react-query/src/suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ensureSuspenseTimers = (
if (defaultedOptions.suspense) {
// Always set stale time when using suspense to prevent
// fetching again when directly mounting after suspending
if (typeof defaultedOptions.staleTime !== 'number') {
if (defaultedOptions.staleTime === undefined) {
defaultedOptions.staleTime = 1000
}
if (typeof defaultedOptions.gcTime === 'number') {
Expand Down
Loading