Skip to content

Commit aa288a8

Browse files
committed
Avoid passing an identity function to createSelector
1 parent e1d71be commit aa288a8

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,6 @@ export type MutationTrigger<D extends MutationDefinition<any, any, any, any>> =
541541
(arg: QueryArgFrom<D>): MutationActionCreatorResult<D>
542542
}
543543

544-
const defaultQueryStateSelector: QueryStateSelector<any, any> = (x) => x
545-
const defaultMutationStateSelector: MutationStateSelector<any, any> = (x) => x
546-
547544
/**
548545
* Wrapper around `defaultQueryStateSelector` to be used in `useQuery`.
549546
* We want the initial render to already come back with
@@ -989,10 +986,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
989986
}
990987

991988
function buildMutationHook(name: string): UseMutation<any> {
992-
return ({
993-
selectFromResult = defaultMutationStateSelector,
994-
fixedCacheKey,
995-
} = {}) => {
989+
return ({ selectFromResult, fixedCacheKey } = {}) => {
996990
const { select, initiate } = api.endpoints[name] as ApiEndpointMutation<
997991
MutationDefinition<any, any, any, any, any>,
998992
Definitions
@@ -1019,13 +1013,16 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
10191013
)
10201014

10211015
const { requestId } = promise || {}
1016+
const initialSelector = useMemo(
1017+
() => select({ fixedCacheKey, requestId: promise?.requestId }),
1018+
[fixedCacheKey, promise, select]
1019+
)
10221020
const mutationSelector = useMemo(
10231021
() =>
1024-
createSelector(
1025-
[select({ fixedCacheKey, requestId: promise?.requestId })],
1026-
selectFromResult
1027-
),
1028-
[select, promise, selectFromResult, fixedCacheKey]
1022+
selectFromResult
1023+
? createSelector([initialSelector], selectFromResult)
1024+
: initialSelector,
1025+
[selectFromResult, initialSelector]
10291026
)
10301027

10311028
const currentState = useSelector(mutationSelector, shallowEqual)

packages/toolkit/tsconfig.base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
// for type imports in tests only
4444
"@reduxjs/toolkit/dist/query/*": ["src/query/*"], // @remap-prod-remove-line
4545
// internal imports in tests only
46-
"@internal/*": ["src/*"]
46+
"@internal/*": ["src/*"],
47+
"react": ["node_modules/react"]
4748
}
4849
}
4950
}

0 commit comments

Comments
 (0)