Skip to content

Commit 94c3e9e

Browse files
committed
fix: only keep notify on stale option
1 parent f559d25 commit 94c3e9e

File tree

6 files changed

+23
-32
lines changed

6 files changed

+23
-32
lines changed

docs/src/pages/reference/useQuery.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,13 @@ const result = useQuery({
110110
- If set to `true`, the query will refetch on reconnect if the data is stale.
111111
- If set to `false`, the query will not refetch on reconnect.
112112
- If set to `"always"`, the query will always refetch on reconnect.
113-
- `notifyOnFetchChange: boolean`
114-
- Optional
115-
- Set this to `false` to prevent re-renders when the `isFetching` or `failureCount` properties change.
116-
- Defaults to `true`.
117113
- `notifyOnStaleChange: boolean`
118114
- Optional
119115
- Set this to `true` to re-render when the `isStale` property changes.
120116
- Defaults to `false`.
121117
- `notifyOnStatusChange: boolean`
122118
- Optional
123-
- Set this to `false` to prevent re-renders when the `status` property changes.
119+
- Set this to `false` to only re-render when there are changes to `data` or `error`.
124120
- Defaults to `true`.
125121
- `onSuccess: (data: TData) => void`
126122
- Optional

src/core/queryObserver.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ export class QueryObserver<
249249
this.staleTimeoutId = setTimeout(() => {
250250
if (!this.currentResult.isStale) {
251251
this.updateResult()
252-
this.notify({ listeners: true, cache: true })
252+
this.notify({
253+
listeners: this.options.notifyOnStaleChange === true,
254+
cache: true,
255+
})
253256
}
254257
}, timeout)
255258
}
@@ -446,19 +449,11 @@ export class QueryObserver<
446449
}
447450

448451
if (
449-
// Always notify on data or error changes
452+
// Always notify if notifyOnStatusChange is set
453+
this.options.notifyOnStatusChange !== false ||
454+
// Otherwise only notify on data or error change
450455
currentResult.data !== prevResult.data ||
451-
currentResult.error !== prevResult.error ||
452-
// Maybe notify on status change
453-
(this.options.notifyOnStatusChange !== false &&
454-
currentResult.status !== prevResult.status) ||
455-
// Maybe notify on fetch change
456-
(this.options.notifyOnFetchChange !== false &&
457-
(currentResult.isFetching !== prevResult.isFetching ||
458-
currentResult.failureCount !== prevResult.failureCount)) ||
459-
// Maybe notify on stale change
460-
(this.options.notifyOnStaleChange &&
461-
currentResult.isStale !== prevResult.isStale)
456+
currentResult.error !== prevResult.error
462457
) {
463458
notifyOptions.listeners = true
464459
}

src/core/tests/queryCache.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,10 @@ describe('queryCache', () => {
584584
const key = queryKey()
585585
const queryFn = jest.fn()
586586
const testCache = new QueryCache()
587-
const testClient = new QueryClient({ queryCache: testCache })
587+
const testClient = new QueryClient({
588+
queryCache: testCache,
589+
defaultOptions: { queries: { notifyOnStaleChange: true } },
590+
})
588591
const observer = new QueryObserver(testClient, {
589592
queryKey: key,
590593
enabled: false,

src/core/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,14 @@ export interface QueryObserverOptions<
124124
* Defaults to `true`.
125125
*/
126126
refetchOnMount?: boolean | 'always'
127-
/**
128-
* Whether a component should re-render when the `isFetching` or `failureCount` properties change.
129-
* Defaults to `true`.
130-
*/
131-
notifyOnFetchChange?: boolean
132127
/**
133128
* Whether a component should re-render when the `isStale` property changes.
134129
* Defaults to `false`.
135130
*/
136131
notifyOnStaleChange?: boolean
137132
/**
138-
* Whether a component should re-render when the `status` property changes.
133+
* Whether a change to the query status should re-render a component.
134+
* If set to `false`, the component will only re-render when the actual `data` or `error` changes.
139135
* Defaults to `true`.
140136
*/
141137
notifyOnStatusChange?: boolean

src/react/tests/useInfiniteQuery.test.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ describe('useInfiniteQuery', () => {
4545
queryCache,
4646
defaultOptions: {
4747
queries: {
48-
notifyOnFetchChange: true,
4948
notifyOnStaleChange: true,
50-
notifyOnStatusChange: true,
5149
},
5250
},
5351
})
@@ -633,7 +631,7 @@ describe('useInfiniteQuery', () => {
633631

634632
await sleep(300)
635633

636-
expect(states.length).toBe(4)
634+
expect(states.length).toBe(5)
637635
expect(states[0]).toMatchObject({
638636
hasNextPage: undefined,
639637
data: undefined,
@@ -656,6 +654,13 @@ describe('useInfiniteQuery', () => {
656654
isSuccess: true,
657655
})
658656
expect(states[3]).toMatchObject({
657+
hasNextPage: true,
658+
data: { pages: [10] },
659+
isFetching: true,
660+
isFetchingNextPage: true,
661+
isSuccess: true,
662+
})
663+
expect(states[4]).toMatchObject({
659664
hasNextPage: true,
660665
data: { pages: [10, 11] },
661666
isFetching: false,

src/react/tests/useQuery.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ describe('useQuery', () => {
1818
queryCache,
1919
defaultOptions: {
2020
queries: {
21-
notifyOnFetchChange: true,
2221
notifyOnStaleChange: true,
23-
notifyOnStatusChange: true,
2422
},
2523
},
2624
})
@@ -524,7 +522,6 @@ describe('useQuery', () => {
524522
function Page() {
525523
const state = useQuery(key, () => ({ name: 'test' }), {
526524
select: data => data.name,
527-
notifyOnFetchChange: false,
528525
notifyOnStaleChange: false,
529526
notifyOnStatusChange: false,
530527
})
@@ -1377,7 +1374,6 @@ describe('useQuery', () => {
13771374
},
13781375
{
13791376
notifyOnStatusChange: false,
1380-
notifyOnFetchChange: false,
13811377
notifyOnStaleChange: false,
13821378
}
13831379
)

0 commit comments

Comments
 (0)