Skip to content

Commit 43e223a

Browse files
authored
Fix refetchInterval infinite loop (#404)
* use queryRef to avoid infinite loop in refetch interval effect * fix original config being mutated by config from another hook instance * store refetchIntervalId and fix refetchInterval comparison * clear query.currentRefetchInterval on cleanup
1 parent 6bf1f30 commit 43e223a

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/queryCache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function makeQueryCache() {
110110

111111
if (query) {
112112
Object.assign(query, { queryVariables, queryFn })
113-
Object.assign(query.config, config)
113+
query.config = { ...query.config, ...config }
114114
} else {
115115
query = makeQuery({
116116
queryKey,

src/useBaseQuery.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,28 @@ export function useBaseQuery(queryKey, queryVariables, queryFn, config = {}) {
8989

9090
// Handle refetch interval
9191
React.useEffect(() => {
92+
const query = queryRef.current
9293
if (
9394
config.refetchInterval &&
94-
(!query.refetchInterval || config.refetchInterval < query.refetchInterval)
95+
(!query.currentRefetchInterval ||
96+
// shorter interval should override previous one
97+
config.refetchInterval < query.currentRefetchInterval)
9598
) {
96-
clearInterval(query.refetchInterval)
97-
query.refetchInterval = setInterval(() => {
99+
query.currentRefetchInterval = config.refetchInterval
100+
clearInterval(query.refetchIntervalId)
101+
query.refetchIntervalId = setInterval(() => {
98102
if (isDocumentVisible() || config.refetchIntervalInBackground) {
99103
refetch().catch(Console.error)
100104
}
101105
}, config.refetchInterval)
102106

103107
return () => {
104-
clearInterval(query.refetchInterval)
105-
delete query.refetchInterval
108+
clearInterval(query.refetchIntervalId)
109+
delete query.refetchIntervalId
110+
delete query.currentRefetchInterval
106111
}
107112
}
108-
}, [
109-
config.refetchInterval,
110-
config.refetchIntervalInBackground,
111-
query.refetchInterval,
112-
refetch,
113-
])
113+
}, [config.refetchInterval, config.refetchIntervalInBackground, refetch])
114114

115115
return {
116116
...query.state,

0 commit comments

Comments
 (0)