-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
data
instance
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? |
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 |
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. |
Expect this to ship in a feature release soon. |
That was fast 😄. Thanks, I'll keep an eye on the next release! |
The
data
object returned byuseQuery
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 auseEffect
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
andreact-query
on line 18 and check in the console to see howmemo
is triggered every time a request is done.The text was updated successfully, but these errors were encountered: