Skip to content

Commit

Permalink
add currentData property to hook results. (#1500)
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 1, 2021
1 parent 95765a5 commit 18ef51d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ export type UseQueryStateResult<

type UseQueryStateBaseResult<D extends QueryDefinition<any, any, any, any>> =
QuerySubState<D> & {
/**
* Where `data` tries to hold data as much as possible, also re-using
* data from the last arguments passed into the hook, this property
* will always contain the received data from the query, for the current query arguments.
*/
currentData?: ResultTypeFrom<D>
/**
* Query has not started yet.
*/
Expand Down Expand Up @@ -342,11 +348,21 @@ type UseQueryStateDefaultResult<D extends QueryDefinition<any, any, any, any>> =
| { isLoading: true; isFetching: boolean; data: undefined }
| ({
isSuccess: true
isFetching: boolean
isFetching: true
error: undefined
} & Required<
Pick<UseQueryStateBaseResult<D>, 'data' | 'fulfilledTimeStamp'>
>)
| ({
isSuccess: true
isFetching: false
error: undefined
} & Required<
Pick<
UseQueryStateBaseResult<D>,
'data' | 'fulfilledTimeStamp' | 'currentData'
>
>)
| ({ isError: true } & Required<
Pick<UseQueryStateBaseResult<D>, 'error'>
>)
Expand Down Expand Up @@ -421,6 +437,7 @@ const queryStatePreSelector = (
return {
...currentState,
data,
currentData: currentState.data,
isFetching,
isLoading,
isSuccess,
Expand Down
14 changes: 14 additions & 0 deletions packages/toolkit/src/query/tests/unionTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ describe.skip('TS only tests', () => {
expectExactType(false as false)(result.isError)
}

expectExactType('' as string | undefined)(result.currentData)
// @ts-expect-error
expectExactType('' as string)(result.currentData)

if (result.isSuccess) {
if (!result.isFetching) {
expectExactType('' as string)(result.currentData)
} else {
expectExactType('' as string | undefined)(result.currentData)
// @ts-expect-error
expectExactType('' as string)(result.currentData)
}
}

// @ts-expect-error
expectType<never>(result)
// is always one of those four
Expand Down

0 comments on commit 18ef51d

Please sign in to comment.