Skip to content

Commit ad4696c

Browse files
improve stability of useInfiniteQuerySubscription's return value (#4937)
* improve stability of useInfiniteQuerySubscription's return value * Ensure refetch is stable --------- Co-authored-by: Mark Erikson <[email protected]>
1 parent ad2b288 commit ad4696c

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -2052,25 +2052,42 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
20522052

20532053
usePromiseRefUnsubscribeOnUnmount(promiseRef)
20542054

2055+
const stableArg = useStableQueryArgs(
2056+
options.skip ? skipToken : arg,
2057+
// Even if the user provided a per-endpoint `serializeQueryArgs` with
2058+
// a consistent return value, _here_ we want to use the default behavior
2059+
// so we can tell if _anything_ actually changed. Otherwise, we can end up
2060+
// with a case where the query args did change but the serialization doesn't,
2061+
// and then we never try to initiate a refetch.
2062+
defaultSerializeQueryArgs,
2063+
context.endpointDefinitions[endpointName],
2064+
endpointName,
2065+
)
2066+
2067+
const refetch = useCallback(
2068+
() => refetchOrErrorIfUnmounted(promiseRef),
2069+
[promiseRef],
2070+
)
2071+
20552072
return useMemo(() => {
20562073
const fetchNextPage = () => {
2057-
return trigger(arg, 'forward')
2074+
return trigger(stableArg, 'forward')
20582075
}
20592076

20602077
const fetchPreviousPage = () => {
2061-
return trigger(arg, 'backward')
2078+
return trigger(stableArg, 'backward')
20622079
}
20632080

20642081
return {
20652082
trigger,
20662083
/**
20672084
* A method to manually refetch data for the query
20682085
*/
2069-
refetch: () => refetchOrErrorIfUnmounted(promiseRef),
2086+
refetch,
20702087
fetchNextPage,
20712088
fetchPreviousPage,
20722089
}
2073-
}, [promiseRef, trigger, arg])
2090+
}, [refetch, trigger, stableArg])
20742091
}
20752092

20762093
const useInfiniteQueryState: UseInfiniteQueryState<any> =

0 commit comments

Comments
 (0)