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
rival-blackOP•16mo ago
I could also refcount myself if I had a hook for each useQuery hook unmounting
rival-blackOP•16mo 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
xenial-black•16mo ago
you'd need to subscribe to the cache directly and listen to the queryObserverRemoved event
queryClient.getQueryCache().subscribe
rival-blackOP•16mo ago
Thanks @TkDodo 🔮 ❤️