Skip to content

Commit

Permalink
refactor: only pass query to refetchInterval function (#6128)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TkDodo authored Oct 12, 2023
1 parent f9164ed commit 403699a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
11 changes: 11 additions & 0 deletions docs/react/guides/migrating-to-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ A few notes about how codemod works:

`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.

### The `refetchInteval` callback function only gets `query` passed

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`.

```diff
- refetchInterval: number | false | ((data: TData | undefined, query: Query) => number | false | undefined)
+ refetchInterval: number | false | ((query: Query) => number | false | undefined)
```

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`.

### The `remove` method has been removed from useQuery

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.
Expand Down
4 changes: 2 additions & 2 deletions docs/react/reference/useQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ const {
- `queryKeyHashFn: (queryKey: QueryKey) => string`
- Optional
- If specified, this function is used to hash the `queryKey` to a string.
- `refetchInterval: number | false | ((data: TData | undefined, query: Query) => number | false | undefined)`
- `refetchInterval: number | false | ((query: Query) => number | false | undefined)`
- Optional
- If set to a number, all queries will continuously refetch at this frequency in milliseconds
- If set to a function, the function will be executed with the latest data and query to compute a frequency
- If set to a function, the function will be executed with the query to compute a frequency
- `refetchIntervalInBackground: boolean`
- Optional
- 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
Expand Down
5 changes: 1 addition & 4 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,7 @@ export class QueryObserver<
#computeRefetchInterval() {
return (
(typeof this.options.refetchInterval === 'function'
? this.options.refetchInterval(
this.#currentResult.data,
this.#currentQuery,
)
? this.options.refetchInterval(this.#currentQuery)
: this.options.refetchInterval) ?? false
)
}
Expand Down
1 change: 0 additions & 1 deletion packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ export interface QueryObserverOptions<
| number
| false
| ((
data: TData | undefined,
query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
) => number | false | undefined)
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/react-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4146,7 +4146,7 @@ describe('useQuery', () => {
await sleep(10)
return count++
},
refetchInterval: (data = 0) => (data < 2 ? 10 : false),
refetchInterval: ({ state: { data = 0 } }) => (data < 2 ? 10 : false),
})

states.push(queryInfo)
Expand Down
16 changes: 9 additions & 7 deletions packages/react-query/src/__tests__/useQuery.types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ describe('initialData', () => {

it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
doNotExecute(() => {
const options = <TData = number,>(select?: (data: number) => TData) =>
queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(1),
select,
})
const query = useQuery(options((data) => data > 1))
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(1),
})

const query = useQuery({
...options,
select: (data) => data > 1,
})

const result: Expect<
Equal<boolean | undefined, (typeof query)['data']>
Expand Down
2 changes: 1 addition & 1 deletion packages/solid-query/src/__tests__/createQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3715,7 +3715,7 @@ describe('createQuery', () => {
await sleep(10)
return count++
},
refetchInterval: (data = 0) => (data < 2 ? 10 : false),
refetchInterval: ({ state: { data = 0 } }) => (data < 2 ? 10 : false),
}))

createRenderEffect(() => {
Expand Down

0 comments on commit 403699a

Please sign in to comment.