Replies: 4 comments 8 replies
-
Data returned by queries must be stable. If not, it would cause too many side effects and unnecessary memoizations throughout your application. As with most data models in react, immutability is implied unless stated otherwise. With that said, gel free to open a PR updating the docs with any information you feel would have helped you. |
Beta Was this translation helpful? Give feedback.
-
Here's a contrived example:
Ok, I named the component poorly, but that last SortedList will end up sorted by date and value rather than in the original order. This could be solved by doing something like |
Beta Was this translation helpful? Give feedback.
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
@webarnes managed to manipulate the data without changing the values in another instance? |
Beta Was this translation helpful? Give feedback.
-
This is slightly related to an older discussion (#530). useQuery currently returns the object from the cache by reference and any operations you perform on it affect other instances. In my case, for example, I have a list that is fetched from the server and used in a few places. In two places, it is sorted and since Array.sort() modifies the array, the sorting in one component modifies the order of the data in a completely different component.
Currently, I'm just careful about cloning any array returned by useQuery before modifying it, but I wonder if there is really any use case for returning the array by reference. It would probably avoid a lot of bugs if useQuery returned copies. I suppose I could have the queryFn return an Immutable but it strikes me as more reasonable for react-query to clone anything being returned from the cache.
In the alternative, it should at least be in the documentation so that it doesn't catch people by surprise.
Beta Was this translation helpful? Give feedback.
All reactions