Skip to content

Conversation

@cherniavskii
Copy link
Contributor

Use queryRef to avoid infinite loop in refetch interval effect.

Fixes #399

const query = queryRef.current
if (
config.refetchInterval &&
(!query.refetchInterval || config.refetchInterval < query.refetchInterval)
Copy link
Contributor Author

@cherniavskii cherniavskii Apr 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think query.refetchInterval name is confusing and should be renamed to query.refetchIntervalId, since it actually stores interval ID.

Then config.refetchInterval < query.refetchInterval comparison doesn't make sense to me.

As well as !query.refetchInterval check, since query.refetchInterval will be always undefined due to cleaning function returned by useEffect.

@tannerlinsley thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the idea was that if a user passed different intervals for the same query but on different instances, then the shortest interval time would override the longer one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should work as you described now!

@cherniavskii
Copy link
Contributor Author

Wanted to add a unit test fot this case, but it involves dealing with timers, which can be tricky and I did't want to invest my time into this right now.
So I've created an issue - #405
Please add "help wanted" label to it :)

if (query) {
Object.assign(query, { queryVariables, queryFn })
Object.assign(query.config, config)
query.config = { ...query.config, ...config }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannerlinsley

Object.assign was causing a bug. Consider this situation:

  1. first instance of useQuery creates a new query and sets query.config pointer to config object
  2. second instance of useQuery with different config object takes query from cache
  3. Object.assign mutates first instance query.config object

Now as I think about it, maybe we should copy query config in the first step instead of referencing it?
Either way, it should be good now.

But that was super tough to debug :D

@tannerlinsley
Copy link
Collaborator

Yes!! This was a total doozy

@tannerlinsley tannerlinsley merged commit 43e223a into TanStack:master May 1, 2020
@cherniavskii cherniavskii deleted the fix/refetchInterval-infinite-loop branch May 1, 2020 11:31
@nx-cloud
Copy link

nx-cloud bot commented Jun 26, 2025

View your CI Pipeline Execution ↗ for commit cc9fd67

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2025-08-08 14:05:05 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants