#Subscribe to a query without useQuery

10 messages · Page 1 of 1 (latest)

rigid cloak
#

I have a list of 300+ items rendered on the page and want to avoid mounting useQuery for each item. However, each item is wrapped with React.memo and I need the component to update whenever one particular query's data is updated. is there a way to subscribe to one particular query's changes and update the component accordingly without performance issues? thanks!

glossy hamlet
#

Why would useQuery have performance issues?

rigid cloak
#

@glossy hamlet Our lighthouse / performance tab often shows scripting / performance issues when mounting 400+ query observers on a page with a list. my team has been relying on mounting a query on the top level of the app and then syncing the data to jotai instead. unfortunately, each list item uses a different query observer and we don't want to be syncing to jotai for all the observers used. we are still on v4, not sure if v5 made improvements regarding mounting a lot of observers!

glossy hamlet
#

It compares rtkq, react-query and I think swr

#

There is an overhead with creating observers, but I wouldn't expect it to show with a couple of hundreds. Maybe I'm wrong though-can you create a codesandbox that shows this behavior?

rigid cloak
#

Let me try to create one to show it!Thanks!

Is there a way to reuse an existing observer if a observer with the same query key is already mounted?

glossy hamlet
#

no, an observer is unique per component. You can only move the observer higher up the tree and then pass data down.

I can tell more once I see the reproduction. You can also do direct cache subscriptions, which likely don't have the overhead, but that's not what it was made for. A lot of features are tied to observers. For example, without an active observer, your query is eligible for garbage collection, even if you read data in your component tree somewhere else. without an observer, you won't get fetches or refetches triggered, staleTime calculations depends on observers etc ...

rigid cloak
#

Thanks! is there an example of direct cache subscription in the doc? It is also likely due to how heavy our list item is as well..