Skip to content

Commit 403699a

Browse files
authored
refactor: only pass query to refetchInterval function (#6128)
* refactor: only pass query to refetchInterval function this streamlines it with other functions, and avoids typing issues as it previously took transformed data from `select` * test: adapt test because we don't have the types limitation anymore * docs: refetchInterval
1 parent f9164ed commit 403699a

File tree

7 files changed

+25
-16
lines changed

7 files changed

+25
-16
lines changed

docs/react/guides/migrating-to-v5.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ A few notes about how codemod works:
136136

137137
`onSuccess`, `onError` and `onSettled` have been removed from Queries. They haven't been touched for Mutations. Please see [this RFC](https://github.com/TanStack/query/discussions/5279) for motivations behind this change and what to do instead.
138138

139+
### The `refetchInteval` callback function only gets `query` passed
140+
141+
This streamlines how callbacks are invoked (the `refetchOnWindowFocus`, `refetchOnMount` and `refetchOnReconnect` callbacks all only get the query passed as well), and it fixes some typing issues when callbacks get data transformed by `select`.
142+
143+
```diff
144+
- refetchInterval: number | false | ((data: TData | undefined, query: Query) => number | false | undefined)
145+
+ refetchInterval: number | false | ((query: Query) => number | false | undefined)
146+
```
147+
148+
You can still access data with `query.state.data`, however, it will not be data that has been transformed by `select`. If you need to access the transformed data, you can call the transformation again on `query.state.data`.
149+
139150
### The `remove` method has been removed from useQuery
140151

141152
Previously, remove method used to remove the query from the queryCache without informing observers about it. It was best used to remove data imperatively that is no longer needed, e.g. when logging a user out.

docs/react/reference/useQuery.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ const {
9898
- `queryKeyHashFn: (queryKey: QueryKey) => string`
9999
- Optional
100100
- If specified, this function is used to hash the `queryKey` to a string.
101-
- `refetchInterval: number | false | ((data: TData | undefined, query: Query) => number | false | undefined)`
101+
- `refetchInterval: number | false | ((query: Query) => number | false | undefined)`
102102
- Optional
103103
- If set to a number, all queries will continuously refetch at this frequency in milliseconds
104-
- If set to a function, the function will be executed with the latest data and query to compute a frequency
104+
- If set to a function, the function will be executed with the query to compute a frequency
105105
- `refetchIntervalInBackground: boolean`
106106
- Optional
107107
- If set to `true`, queries that are set to continuously refetch with a `refetchInterval` will continue to refetch while their tab/window is in the background

packages/query-core/src/queryObserver.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,7 @@ export class QueryObserver<
368368
#computeRefetchInterval() {
369369
return (
370370
(typeof this.options.refetchInterval === 'function'
371-
? this.options.refetchInterval(
372-
this.#currentResult.data,
373-
this.#currentQuery,
374-
)
371+
? this.options.refetchInterval(this.#currentQuery)
375372
: this.options.refetchInterval) ?? false
376373
)
377374
}

packages/query-core/src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ export interface QueryObserverOptions<
237237
| number
238238
| false
239239
| ((
240-
data: TData | undefined,
241240
query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
242241
) => number | false | undefined)
243242
/**

packages/react-query/src/__tests__/useQuery.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4146,7 +4146,7 @@ describe('useQuery', () => {
41464146
await sleep(10)
41474147
return count++
41484148
},
4149-
refetchInterval: (data = 0) => (data < 2 ? 10 : false),
4149+
refetchInterval: ({ state: { data = 0 } }) => (data < 2 ? 10 : false),
41504150
})
41514151

41524152
states.push(queryInfo)

packages/react-query/src/__tests__/useQuery.types.test.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ describe('initialData', () => {
4747

4848
it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
4949
doNotExecute(() => {
50-
const options = <TData = number,>(select?: (data: number) => TData) =>
51-
queryOptions({
52-
queryKey: ['key'],
53-
queryFn: () => Promise.resolve(1),
54-
select,
55-
})
56-
const query = useQuery(options((data) => data > 1))
50+
const options = queryOptions({
51+
queryKey: ['key'],
52+
queryFn: () => Promise.resolve(1),
53+
})
54+
55+
const query = useQuery({
56+
...options,
57+
select: (data) => data > 1,
58+
})
5759

5860
const result: Expect<
5961
Equal<boolean | undefined, (typeof query)['data']>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3715,7 +3715,7 @@ describe('createQuery', () => {
37153715
await sleep(10)
37163716
return count++
37173717
},
3718-
refetchInterval: (data = 0) => (data < 2 ? 10 : false),
3718+
refetchInterval: ({ state: { data = 0 } }) => (data < 2 ? 10 : false),
37193719
}))
37203720

37213721
createRenderEffect(() => {

0 commit comments

Comments
 (0)