Skip to content

Commit 0ebdbfa

Browse files
committed
Revert "clear data on skip" back to its original behavior
1 parent 43d94a0 commit 0ebdbfa

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
626626
)
627627
lastResult = undefined
628628
}
629-
if (queryArgs === skipToken) {
630-
lastResult = undefined
631-
}
629+
632630
// 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
633631
let data = currentState.isSuccess ? currentState.data : lastResult?.data
634632
if (data === undefined) data = currentState.data
@@ -764,6 +762,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
764762
const lastSubscriptionOptions = promiseRef.current?.subscriptionOptions
765763

766764
if (!lastPromise || lastPromise.arg !== stableArg) {
765+
// console.log('Initiating subscription: ', stableArg)
767766
lastPromise?.unsubscribe()
768767
const promise = dispatch(
769768
initiate(stableArg, {

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import util from 'util'
12
import * as React from 'react'
23
import type {
34
UseMutation,
@@ -2472,7 +2473,11 @@ describe('skip behaviour', () => {
24722473
await act(async () => {
24732474
rerender([1, { skip: true }])
24742475
})
2475-
expect(result.current).toEqual(uninitialized)
2476+
expect(result.current).toEqual({
2477+
...uninitialized,
2478+
currentData: undefined,
2479+
data: { name: 'Timmy' },
2480+
})
24762481
await delay(1)
24772482
expect(subscriptionCount('getUser(1)')).toBe(0)
24782483
})
@@ -2489,6 +2494,7 @@ describe('skip behaviour', () => {
24892494

24902495
expect(result.current).toEqual(uninitialized)
24912496
await delay(1)
2497+
24922498
expect(subscriptionCount('getUser(1)')).toBe(0)
24932499
// also no subscription on `getUser(skipToken)` or similar:
24942500
expect(storeRef.store.getState().api.subscriptions).toEqual({})
@@ -2504,10 +2510,51 @@ describe('skip behaviour', () => {
25042510
await act(async () => {
25052511
rerender([skipToken])
25062512
})
2507-
expect(result.current).toEqual(uninitialized)
2513+
expect(result.current).toEqual({
2514+
...uninitialized,
2515+
currentData: undefined,
2516+
data: { name: 'Timmy' },
2517+
})
25082518
await delay(1)
25092519
expect(subscriptionCount('getUser(1)')).toBe(0)
25102520
})
2521+
2522+
test('skipping a previously fetched query retains the existing value as `data`, but clears `currentData`', async () => {
2523+
const { result, rerender } = renderHook(
2524+
([arg, options]: Parameters<typeof api.endpoints.getUser.useQuery>) =>
2525+
api.endpoints.getUser.useQuery(arg, options),
2526+
{
2527+
wrapper: storeRef.wrapper,
2528+
initialProps: [1],
2529+
}
2530+
)
2531+
2532+
await act(async () => {
2533+
await delay(1)
2534+
})
2535+
2536+
// Normal fulfilled result, with both `data` and `currentData`
2537+
expect(result.current).toMatchObject({
2538+
status: QueryStatus.fulfilled,
2539+
isSuccess: true,
2540+
data: { name: 'Timmy' },
2541+
currentData: { name: 'Timmy' },
2542+
})
2543+
2544+
await act(async () => {
2545+
rerender([1, { skip: true }])
2546+
await delay(1)
2547+
})
2548+
2549+
// After skipping, the query is "uninitialized", but still retains the last fetched `data`
2550+
// even though it's skipped. `currentData` is undefined, since that matches the current arg.
2551+
expect(result.current).toMatchObject({
2552+
status: QueryStatus.uninitialized,
2553+
isSuccess: false,
2554+
data: { name: 'Timmy' },
2555+
currentData: undefined,
2556+
})
2557+
})
25112558
})
25122559

25132560
// type tests:

0 commit comments

Comments
 (0)