Skip to content

Commit f8f0b00

Browse files
committed
Deduplicate hook unsubs and refetches
1 parent 312f008 commit f8f0b00

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
15951595
// @ts-ignore
15961596
createSelector(
15971597
[
1598+
// @ts-ignore
15981599
select(stableArg),
15991600
(_: ApiRootState, lastResult: any) => lastResult,
16001601
(_: ApiRootState) => stableArg,
@@ -1640,6 +1641,27 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
16401641
return useQueryState
16411642
}
16421643

1644+
function usePromiseRefUnsubscribeOnUnmount(
1645+
promiseRef: React.RefObject<{ unsubscribe?: () => void } | undefined>,
1646+
) {
1647+
useEffect(() => {
1648+
return () => {
1649+
promiseRef.current?.unsubscribe?.()
1650+
promiseRef.current = undefined
1651+
}
1652+
}, [promiseRef])
1653+
}
1654+
1655+
function refetchOrErrorIfUnmounted<
1656+
T extends
1657+
| QueryActionCreatorResult<any>
1658+
| InfiniteQueryActionCreatorResult<any>,
1659+
>(promiseRef: React.RefObject<T | undefined>): T {
1660+
if (!promiseRef.current)
1661+
throw new Error('Cannot refetch a query that has not been started yet.')
1662+
return promiseRef.current.refetch() as T
1663+
}
1664+
16431665
function buildQueryHooks(endpointName: string): QueryHooks<any> {
16441666
const useQuerySubscription: UseQuerySubscription<any> = (
16451667
arg: any,
@@ -1649,25 +1671,14 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
16491671
QueryActionCreatorResult<any>
16501672
>(endpointName, arg, options)
16511673

1652-
useEffect(() => {
1653-
return () => {
1654-
promiseRef.current?.unsubscribe()
1655-
promiseRef.current = undefined
1656-
}
1657-
}, [promiseRef])
1674+
usePromiseRefUnsubscribeOnUnmount(promiseRef)
16581675

16591676
return useMemo(
16601677
() => ({
16611678
/**
16621679
* A method to manually refetch data for the query
16631680
*/
1664-
refetch: () => {
1665-
if (!promiseRef.current)
1666-
throw new Error(
1667-
'Cannot refetch a query that has not been started yet.',
1668-
)
1669-
return promiseRef.current?.refetch()
1670-
},
1681+
refetch: () => refetchOrErrorIfUnmounted(promiseRef),
16711682
}),
16721683
[promiseRef],
16731684
)
@@ -1851,12 +1862,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
18511862
[promiseRef, dispatch, initiate],
18521863
)
18531864

1854-
useEffect(() => {
1855-
return () => {
1856-
promiseRef.current?.unsubscribe()
1857-
promiseRef.current = undefined
1858-
}
1859-
}, [promiseRef])
1865+
usePromiseRefUnsubscribeOnUnmount(promiseRef)
18601866

18611867
return useMemo(() => {
18621868
const fetchNextPage = () => {
@@ -1875,13 +1881,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
18751881
/**
18761882
* A method to manually refetch data for the query
18771883
*/
1878-
refetch: () => {
1879-
if (!promiseRef.current)
1880-
throw new Error(
1881-
'Cannot refetch a query that has not been started yet.',
1882-
)
1883-
return promiseRef.current?.refetch()
1884-
},
1884+
refetch: () => refetchOrErrorIfUnmounted(promiseRef),
18851885
fetchNextPage,
18861886
fetchPreviousPage,
18871887
}

0 commit comments

Comments
 (0)