Skip to content

Commit 6f1f0a4

Browse files
committed
fix skipToken behaviour in useQueryState
1 parent daf8d9a commit 6f1f0a4

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

packages/toolkit/src/query/core/buildSlice.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,19 @@ export function buildSlice({
428428
draft[queryCacheKey]![requestId] = options
429429
}
430430
return true
431-
} else if (unsubscribeQueryResult.match(action)) {
431+
}
432+
if (unsubscribeQueryResult.match(action)) {
432433
const { queryCacheKey, requestId } = action.payload
433434
if (draft[queryCacheKey]) {
434435
delete draft[queryCacheKey]![requestId]
435436
}
436437
return true
437-
} else if (querySlice.actions.removeQueryResult.match(action)) {
438+
}
439+
if (querySlice.actions.removeQueryResult.match(action)) {
438440
delete draft[action.payload.queryCacheKey]
439-
} else if (queryThunk.pending.match(action)) {
441+
return true
442+
}
443+
if (queryThunk.pending.match(action)) {
440444
const {
441445
meta: { arg, requestId },
442446
} = action
@@ -447,7 +451,8 @@ export function buildSlice({
447451

448452
return true
449453
}
450-
} else if (queryThunk.rejected.match(action)) {
454+
}
455+
if (queryThunk.rejected.match(action)) {
451456
const {
452457
meta: { condition, arg, requestId },
453458
} = action

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,9 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
625625
)
626626
lastResult = undefined
627627
}
628-
628+
if (queryArgs === skipToken) {
629+
lastResult = undefined
630+
}
629631
// data is the last known good request result we have tracked - or if none has been tracked yet the last good result for the current args
630632
let data = currentState.isSuccess ? currentState.data : lastResult?.data
631633
if (data === undefined) data = currentState.data

packages/toolkit/src/query/tests/buildHooks.test.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,11 +2379,7 @@ describe('skip behaviour', () => {
23792379
await act(async () => {
23802380
rerender([1, { skip: true }])
23812381
})
2382-
expect(result.current).toEqual({
2383-
...uninitialized,
2384-
currentData: undefined,
2385-
data: { name: 'Timmy' },
2386-
})
2382+
expect(result.current).toEqual(uninitialized)
23872383
await delay(1)
23882384
expect(subscriptionCount('getUser(1)')).toBe(0)
23892385
})
@@ -2415,11 +2411,7 @@ describe('skip behaviour', () => {
24152411
await act(async () => {
24162412
rerender([skipToken])
24172413
})
2418-
expect(result.current).toEqual({
2419-
...uninitialized,
2420-
currentData: undefined,
2421-
data: { name: 'Timmy' },
2422-
})
2414+
expect(result.current).toEqual(uninitialized)
24232415
await delay(1)
24242416
expect(subscriptionCount('getUser(1)')).toBe(0)
24252417
})

0 commit comments

Comments
 (0)