Skip to content

Commit 36fde17

Browse files
committed
Add skip condition on endpoint definition
1 parent 4f3bc9f commit 36fde17

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
523523
getPendingMeta() {
524524
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true }
525525
},
526-
condition(queryThunkArgs, { getState }) {
526+
condition(queryThunkArgs, options) {
527+
const { getState } = options
527528
const state = getState()
528529

529530
const requestState =
@@ -551,6 +552,14 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
551552
return true
552553
}
553554

555+
if (
556+
isQueryDefinition(endpointDefinition) &&
557+
endpointDefinition?.skipCondition &&
558+
endpointDefinition?.skipCondition(state)
559+
) {
560+
return false
561+
}
562+
554563
if (
555564
isQueryDefinition(endpointDefinition) &&
556565
endpointDefinition?.forceRefetch?.({

packages/toolkit/src/query/endpointDefinitions.ts

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030
UnwrapPromise,
3131
} from './tsHelpers'
3232
import { isNotNullish } from './utils'
33+
import type { QueryThunkArg, ThunkApiMetaConfig } from './core/buildThunks'
3334

3435
const resultType = /* @__PURE__ */ Symbol()
3536
const baseQuery = /* @__PURE__ */ Symbol()
@@ -349,6 +350,8 @@ export interface QueryExtraOptions<
349350
*/
350351
invalidatesTags?: never
351352

353+
skipCondition?: (state: RootState<any, string, ReducerPath>) => boolean
354+
352355
/**
353356
* Can be provided to return a custom cache key value based on the query arguments.
354357
*

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { UNINITIALIZED_VALUE } from './constants'
5151
import type { ReactHooksModuleOptions } from './module'
5252
import { useStableQueryArgs } from './useSerializedStableValue'
5353
import { useShallowStableValue } from './useShallowStableValue'
54+
import { isQueryDefinition } from '../endpointDefinitions'
5455

5556
// Copy-pasted from React-Redux
5657
const canUseDOM = () =>
@@ -1316,9 +1317,17 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
13161317
},
13171318
useQuery(arg, options) {
13181319
const querySubscriptionResults = useQuerySubscription(arg, options)
1320+
const store = useStore<RootState<Definitions, any, any>>()
1321+
1322+
const endpointDefinition = context.endpointDefinitions[name]
1323+
const shouldSkipFromCondition =
1324+
isQueryDefinition(endpointDefinition) &&
1325+
endpointDefinition?.skipCondition &&
1326+
endpointDefinition?.skipCondition(store.getState())
1327+
13191328
const queryStateResults = useQueryState(arg, {
13201329
selectFromResult:
1321-
arg === skipToken || options?.skip
1330+
arg === skipToken || options?.skip || shouldSkipFromCondition
13221331
? undefined
13231332
: noPendingQueryStateSelector,
13241333
...options,

0 commit comments

Comments
 (0)