Skip to content

Reference to useQuery is changing on every re-render #1816

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

Closed
dusty opened this issue Feb 17, 2021 · 2 comments
Closed

Reference to useQuery is changing on every re-render #1816

dusty opened this issue Feb 17, 2021 · 2 comments

Comments

@dusty
Copy link

dusty commented Feb 17, 2021

Describe the bug
This is more an FYI, not sure what the intention is here.

In 3.8.3 the return of useQuery was stable between re-renders, but in the 3.9 series, it is no longer. This caused an issue where we had a useMemo that only used that object as a dependency.

eg:

const query = useQuery(...)
const results = useMemo(() => {
  return query.data?.stuff
}, [query])

<OtherThing results={results} />

This was fixed in our code, by updating the useMemo dependency.

const query = useQuery(...)
const results = useMemo(() => {
  return query.data?.stuff
}, [query.data])

<OtherThing results={results} />

To Reproduce
Steps to reproduce the behavior in 3.9.7:

  1. Go to https://codesandbox.io/s/goofy-williams-cie1d?file=/src/index.js
  2. Click on the button with the 0 in it
  3. Watch the console log increment "query changed"

Steps to reproduce the old behavior in 3.8.3:

  1. Go to https://codesandbox.io/s/hungry-gareth-sexnh?file=/src/index.js:543-548
  2. Click on the button with the 0 in it
  3. Watch the console log NOT increment "query changed"

Expected behavior
I expected the behavior to not change

Screenshots
2021-02-16 17 00 15

@TkDodo
Copy link
Collaborator

TkDodo commented Feb 17, 2021

Yes, this was noted in the PR: #1775

i think it is best to keep the dependencies as narrow as possible. If you only use data, only depend on data. Note that the queryInfo object was also a new reference on background refetches before (not on every render though) - because the isFetching flag changes. So you were likely recomputing that too often before as well.

@dusty
Copy link
Author

dusty commented Feb 17, 2021

Thanks. I just wanted to point it out in case it was unknown.

@dusty dusty closed this as completed Feb 17, 2021
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

No branches or pull requests

2 participants