Can I get notified that all useQuery hooks for a query key have unmounted?
Or can I set gcTime to 0 and get notified of that?
I'm using fetch functions that start subscribing to data on a WebSocket and I'd like to know when to unsubscribe.
4 Replies
constant-blueOP•2y ago
I could also refcount myself if I had a hook for each useQuery hook unmounting
constant-blueOP•2y ago
As usual there's a perfectly good approach described in https://tkdodo.eu/blog/using-web-sockets-with-react-query but it'd be slick to use the existing subscription state.
Using WebSockets with React Query
A step-by-step guide on how to make real-time notifications work with react-query
rival-black•2y ago
you'd need to subscribe to the cache directly and listen to the queryObserverRemoved event
queryClient.getQueryCache().subscribeconstant-blueOP•2y ago
Thanks @TkDodo 🔮 ❤️