Skip to content

Commit 2124217

Browse files
committed
Deduplicate debug values
1 parent 6752226 commit 2124217

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

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

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,23 @@ const noPendingQueryStateSelector: QueryStateSelector<any, any> = (
12111211
return selected
12121212
}
12131213

1214+
function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
1215+
const ret: any = {}
1216+
keys.forEach((key) => {
1217+
ret[key] = obj[key]
1218+
})
1219+
return ret
1220+
}
1221+
1222+
const COMMON_HOOK_DEBUG_FIELDS = [
1223+
'data',
1224+
'status',
1225+
'isLoading',
1226+
'isSuccess',
1227+
'isError',
1228+
'error',
1229+
] as const
1230+
12141231
type GenericPrefetchThunk = (
12151232
endpointName: any,
12161233
arg: any,
@@ -1760,9 +1777,8 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
17601777
...options,
17611778
})
17621779

1763-
const { data, status, isLoading, isSuccess, isError, error } =
1764-
queryStateResults
1765-
useDebugValue({ data, status, isLoading, isSuccess, isError, error })
1780+
const debugValue = pick(queryStateResults, ...COMMON_HOOK_DEBUG_FIELDS)
1781+
useDebugValue(debugValue)
17661782

17671783
return useMemo(
17681784
() => ({ ...queryStateResults, ...querySubscriptionResults }),
@@ -2060,26 +2076,13 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
20602076
...options,
20612077
})
20622078

2063-
const {
2064-
data,
2065-
status,
2066-
isLoading,
2067-
isSuccess,
2068-
isError,
2069-
error,
2070-
hasNextPage,
2071-
hasPreviousPage,
2072-
} = queryStateResults
2073-
useDebugValue({
2074-
data,
2075-
status,
2076-
isLoading,
2077-
isSuccess,
2078-
isError,
2079-
error,
2080-
hasNextPage,
2081-
hasPreviousPage,
2082-
})
2079+
const debugValue = pick(
2080+
queryStateResults,
2081+
...COMMON_HOOK_DEBUG_FIELDS,
2082+
'hasNextPage',
2083+
'hasPreviousPage',
2084+
)
2085+
useDebugValue(debugValue)
20832086

20842087
return useMemo(
20852088
() => ({
@@ -2153,24 +2156,12 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
21532156
})
21542157
}, [dispatch, fixedCacheKey, promise, requestId])
21552158

2156-
const {
2157-
endpointName,
2158-
data,
2159-
status,
2160-
isLoading,
2161-
isSuccess,
2162-
isError,
2163-
error,
2164-
} = currentState
2165-
useDebugValue({
2166-
endpointName,
2167-
data,
2168-
status,
2169-
isLoading,
2170-
isSuccess,
2171-
isError,
2172-
error,
2173-
})
2159+
const debugValue = pick(
2160+
currentState,
2161+
...COMMON_HOOK_DEBUG_FIELDS,
2162+
'endpointName',
2163+
)
2164+
useDebugValue(debugValue)
21742165

21752166
const finalState = useMemo(
21762167
() => ({ ...currentState, originalArgs, reset }),

0 commit comments

Comments
 (0)