Skip to content

Commit 18ef51d

Browse files
authored
add currentData property to hook results. (#1500)
1 parent 95765a5 commit 18ef51d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ export type UseQueryStateResult<
306306

307307
type UseQueryStateBaseResult<D extends QueryDefinition<any, any, any, any>> =
308308
QuerySubState<D> & {
309+
/**
310+
* Where `data` tries to hold data as much as possible, also re-using
311+
* data from the last arguments passed into the hook, this property
312+
* will always contain the received data from the query, for the current query arguments.
313+
*/
314+
currentData?: ResultTypeFrom<D>
309315
/**
310316
* Query has not started yet.
311317
*/
@@ -342,11 +348,21 @@ type UseQueryStateDefaultResult<D extends QueryDefinition<any, any, any, any>> =
342348
| { isLoading: true; isFetching: boolean; data: undefined }
343349
| ({
344350
isSuccess: true
345-
isFetching: boolean
351+
isFetching: true
346352
error: undefined
347353
} & Required<
348354
Pick<UseQueryStateBaseResult<D>, 'data' | 'fulfilledTimeStamp'>
349355
>)
356+
| ({
357+
isSuccess: true
358+
isFetching: false
359+
error: undefined
360+
} & Required<
361+
Pick<
362+
UseQueryStateBaseResult<D>,
363+
'data' | 'fulfilledTimeStamp' | 'currentData'
364+
>
365+
>)
350366
| ({ isError: true } & Required<
351367
Pick<UseQueryStateBaseResult<D>, 'error'>
352368
>)
@@ -421,6 +437,7 @@ const queryStatePreSelector = (
421437
return {
422438
...currentState,
423439
data,
440+
currentData: currentState.data,
424441
isFetching,
425442
isLoading,
426443
isSuccess,

packages/toolkit/src/query/tests/unionTypes.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ describe.skip('TS only tests', () => {
115115
expectExactType(false as false)(result.isError)
116116
}
117117

118+
expectExactType('' as string | undefined)(result.currentData)
119+
// @ts-expect-error
120+
expectExactType('' as string)(result.currentData)
121+
122+
if (result.isSuccess) {
123+
if (!result.isFetching) {
124+
expectExactType('' as string)(result.currentData)
125+
} else {
126+
expectExactType('' as string | undefined)(result.currentData)
127+
// @ts-expect-error
128+
expectExactType('' as string)(result.currentData)
129+
}
130+
}
131+
118132
// @ts-expect-error
119133
expectType<never>(result)
120134
// is always one of those four

0 commit comments

Comments
 (0)