Skip to content

useQuery always return a new "data" instance #430

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
tiagoengel opened this issue May 1, 2020 · 5 comments
Closed

useQuery always return a new "data" instance #430

tiagoengel opened this issue May 1, 2020 · 5 comments

Comments

@tiagoengel
Copy link

tiagoengel commented May 1, 2020

The data object returned by useQuery is always different after a server request is executed, even when the server response is exactly the same.

This can be problematic in some cases, for example using data on a useEffect hook will cause the hook to be executed every time a request is sent, when what I would expect is for it to be triggered only when the actual data changes.

It's also worth noting this is not the case with swr, it will only trigger other hooks when the actual data has changed.

If this is a conscious design decision, it would be great to have it documented. For my current project this is a deal breaker, and it would have saved me some time to know this before hand.

I've created a simple example here https://codesandbox.io/s/react-query-memo-wlw2v?file=/src/App.tsx. You can switch between swr and react-query on line 18 and check in the console to see how memo is triggered every time a request is done.

@tiagoengel tiagoengel changed the title useQuery always return a new data instance useQuery always return a new "data" instance May 1, 2020
@tannerlinsley
Copy link
Collaborator

While I understand what your example is showing I don't think that this is such a good thing on SWR's end. When you fetch new data from a server, you are deserializing a new JSON object, not dealing with a referential pointer to data. That data could have changed in any way really and unless you deep compare the old and new data, you cannot know reliably whether it is the same or not. Deep comparison here would totally kill performance.

So I ask you this, how does SWR know that the data isn't changing? And do you trust it to know reliably and with good performance?

@tiagoengel
Copy link
Author

tiagoengel commented May 1, 2020

I understand your point, and I'm not sure how SWR does it. However, for me the point of using any lib is that you trust it to deal with things you don't want to, and I think super deep and nested server responses are never a good thing anyway, so I don't think it's a bad decision on SWR's end.

But I understand if this is a design decision, in that case it would be great to have this documented, just a very simple example saying a hook depending on useQuery's return will be triggered every time a request is done. I'm sure it will save some headaches 🙂.

@tannerlinsley
Copy link
Collaborator

Upon more investigation, I've found out how they do it. I'm going to do the same actually, but with some better warnings and documentation around it.

@tannerlinsley
Copy link
Collaborator

Expect this to ship in a feature release soon.

@tiagoengel
Copy link
Author

That was fast 😄. Thanks, I'll keep an eye on the next release!

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