Skip to content

Commit 1c03ac8

Browse files
committed
Allow customising createSelector instance used by RTKQ
1 parent ed8282e commit 1c03ac8

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { createNextState, createSelector } from './rtkImports'
1+
import type { createSelector as _createSelector } from './rtkImports'
2+
import { createNextState } from './rtkImports'
23
import type {
34
MutationSubState,
45
QuerySubState,
@@ -123,9 +124,11 @@ export function buildSelectors<
123124
>({
124125
serializeQueryArgs,
125126
reducerPath,
127+
createSelector,
126128
}: {
127129
serializeQueryArgs: InternalSerializeQueryArgs
128130
reducerPath: ReducerPath
131+
createSelector: typeof _createSelector
129132
}) {
130133
type RootState = _RootState<Definitions, string, string>
131134

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import type { ReferenceCacheLifecycle } from './buildMiddleware/cacheLifecycle'
4949
import type { ReferenceQueryLifecycle } from './buildMiddleware/queryLifecycle'
5050
import type { ReferenceCacheCollection } from './buildMiddleware/cacheCollection'
5151
import { enablePatches } from 'immer'
52+
import { createSelector as _createSelector } from './rtkImports'
5253

5354
/**
5455
* `ifOlderThan` - (default: `false` | `number`) - _number is value in seconds_
@@ -431,6 +432,13 @@ export type ListenerActions = {
431432

432433
export type InternalActions = SliceActions & ListenerActions
433434

435+
export interface CoreModuleOptions {
436+
/**
437+
* A selector creator (usually from `reselect`, or matching the same signature)
438+
*/
439+
createSelector?: typeof _createSelector
440+
}
441+
434442
/**
435443
* Creates a module containing the basic redux logic for use with `buildCreateApi`.
436444
*
@@ -439,7 +447,9 @@ export type InternalActions = SliceActions & ListenerActions
439447
* const createBaseApi = buildCreateApi(coreModule());
440448
* ```
441449
*/
442-
export const coreModule = (): Module<CoreModule> => ({
450+
export const coreModule = ({
451+
createSelector = _createSelector,
452+
}: CoreModuleOptions = {}): Module<CoreModule> => ({
443453
name: coreModuleName,
444454
init(
445455
api,
@@ -548,6 +558,7 @@ export const coreModule = (): Module<CoreModule> => ({
548558
} = buildSelectors({
549559
serializeQueryArgs: serializeQueryArgs as any,
550560
reducerPath,
561+
createSelector,
551562
})
552563

553564
safeAssign(api.util, { selectInvalidatedBy, selectCachedArgsForQuery })

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ import { UNINITIALIZED_VALUE } from './constants'
5353
import { useShallowStableValue } from './useShallowStableValue'
5454
import type { BaseQueryFn } from '../baseQueryTypes'
5555
import { defaultSerializeQueryArgs } from '../defaultSerializeQueryArgs'
56-
import {
57-
InternalMiddlewareState,
58-
SubscriptionSelectors,
59-
} from '../core/buildMiddleware/types'
56+
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
6057

6158
// Copy-pasted from React-Redux
6259
export const useIsomorphicLayoutEffect =
@@ -582,6 +579,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
582579
batch,
583580
hooks: { useDispatch, useSelector, useStore },
584581
unstable__sideEffectsInRender,
582+
createSelector,
585583
},
586584
serializeQueryArgs,
587585
context,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import type { QueryKeys } from '../core/apiState'
2424
import type { PrefetchOptions } from '../core/module'
2525
import { countObjectKeys } from '../utils/countObjectKeys'
26+
import { createSelector as _createSelector } from 'reselect'
2627

2728
export const reactHooksModuleName = /* @__PURE__ */ Symbol()
2829
export type ReactHooksModule = typeof reactHooksModuleName
@@ -111,6 +112,10 @@ export interface ReactHooksModuleOptions {
111112
* ```
112113
*/
113114
unstable__sideEffectsInRender?: boolean
115+
/**
116+
* A selector creator (usually from `reselect`, or matching the same signature)
117+
*/
118+
createSelector?: typeof _createSelector
114119
}
115120

116121
/**
@@ -140,6 +145,7 @@ export const reactHooksModule = ({
140145
useSelector: rrUseSelector,
141146
useStore: rrUseStore,
142147
},
148+
createSelector = _createSelector,
143149
unstable__sideEffectsInRender = false,
144150
...rest
145151
}: ReactHooksModuleOptions = {}): Module<ReactHooksModule> => {
@@ -191,6 +197,7 @@ export const reactHooksModule = ({
191197
batch,
192198
hooks,
193199
unstable__sideEffectsInRender,
200+
createSelector,
194201
},
195202
serializeQueryArgs,
196203
context,

0 commit comments

Comments
 (0)