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.
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 useQuery's return will be triggered every time a request is done. I'm sure it will save some headaches 馃檪.
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!
Most helpful comment
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.