Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing context w/o memo to useQuery triggers infinite loop #805

Closed
jakubriedl opened this issue May 11, 2020 · 2 comments
Closed

Passing context w/o memo to useQuery triggers infinite loop #805

jakubriedl opened this issue May 11, 2020 · 2 comments
Labels
duplicate ♊ This issue has already been reported or has been described in another issue

Comments

@jakubriedl
Copy link
Contributor

As mentioned in here passing context object to useQuery triggers infinite request/cancel/request/.. loop.

Passing context like this to triggers infinite request loop

const [{ data }] = useQuery({
  query: someQuery,
  context: {},
})

and changing it to this one fixes that.

const [{ data }] = useQuery({
  query: someQuery,
  context: React.useMemo(() => ({}), []),
})

From the behaviour I assume that somewhere in code the context object is referentialy compared and I would expect either shallow (to match react behaviour) or deep comparison (easier to understand for most devs) of it.

@jakubriedl jakubriedl added the bug 🐛 Oh no! A bug or unintented behaviour. label May 11, 2020
@JoviDeCroock JoviDeCroock added question 🙋 and removed bug 🐛 Oh no! A bug or unintented behaviour. labels May 11, 2020
@JoviDeCroock
Copy link
Collaborator

JoviDeCroock commented May 11, 2020

This has been discussed recently, this is because we shouldn't really be deciding for you whether or not your context change should trigger a refetch, for instance what if you pass in custom fetchOptions that are a function? We can't really decide that function being equal without you using useCallback so we move this responsibility to the user, which in the long run will cause our users less problems.

Some related:

We also have a Spectrum that might be better suited for usage questions.

@jakubriedl
Copy link
Contributor Author

jakubriedl commented May 11, 2020

Thanks for quick explanation. It is fair point that you don't want to decide if it should or should not trigger the re-render. Not really because of functions as they are not even shallow or deep equal but but I get you. I still think shallow equal would be better as it is how components work in react and avoids most the problems you are mentioning.

Would be great to at least document current behaviour as I'm not the first one asking and it is bit unexpected.

@kitten kitten added duplicate ♊ This issue has already been reported or has been described in another issue and removed question 🙋 labels May 11, 2020
@kitten kitten closed this as completed May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate ♊ This issue has already been reported or has been described in another issue
Projects
None yet
Development

No branches or pull requests

3 participants