Skip to content

Commit 00aaf87

Browse files
committed
Add skip condition on endpoint definition
1 parent 3cbb247 commit 00aaf87

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,14 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
919919
return true
920920
}
921921

922+
if (
923+
isQueryDefinition(endpointDefinition) &&
924+
endpointDefinition?.skipCondition &&
925+
endpointDefinition?.skipCondition(state)
926+
) {
927+
return false
928+
}
929+
922930
if (
923931
isQueryDefinition(endpointDefinition) &&
924932
endpointDefinition?.forceRefetch?.({

packages/toolkit/src/query/endpointDefinitions.ts

+12
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,18 @@ export interface QueryExtraOptions<
612612
*/
613613
invalidatesTags?: never
614614

615+
/**
616+
* Can be provided to skip the query if the condition is met.
617+
*
618+
* It's the equivalent of the `skip` option on the query hook but for the endpoint definition.
619+
*
620+
* @example
621+
* ```ts
622+
* skipCondition: (state) => state.user.isLoggedIn === false
623+
* ```
624+
*/
625+
skipCondition?: (state: RootState<any, string, ReducerPath>) => boolean
626+
615627
/**
616628
* Can be provided to return a custom cache key value based on the query arguments.
617629
*

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ import type { ReactHooksModuleOptions } from './module'
6464
import { useStableQueryArgs } from './useSerializedStableValue'
6565
import { useShallowStableValue } from './useShallowStableValue'
6666
import type { InfiniteQueryDirection } from '../core/apiState'
67-
import { isInfiniteQueryDefinition } from '../endpointDefinitions'
67+
import {
68+
isInfiniteQueryDefinition,
69+
isQueryDefinition,
70+
} from '../endpointDefinitions'
6871
import type { StartInfiniteQueryActionCreator } from '../core/buildInitiate'
6972

7073
// Copy-pasted from React-Redux
@@ -1992,9 +1995,17 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
19921995
},
19931996
useQuery(arg, options) {
19941997
const querySubscriptionResults = useQuerySubscription(arg, options)
1998+
const store = useStore<RootState<Definitions, any, any>>()
1999+
2000+
const endpointDefinition = context.endpointDefinitions[endpointName]
2001+
const shouldSkipFromCondition =
2002+
isQueryDefinition(endpointDefinition) &&
2003+
endpointDefinition?.skipCondition &&
2004+
endpointDefinition?.skipCondition(store.getState())
2005+
19952006
const queryStateResults = useQueryState(arg, {
19962007
selectFromResult:
1997-
arg === skipToken || options?.skip
2008+
arg === skipToken || options?.skip || shouldSkipFromCondition
19982009
? undefined
19992010
: noPendingQueryStateSelector,
20002011
...options,

0 commit comments

Comments
 (0)