T
TanStack2y ago
absent-sapphire

useQueryClient ambiguous behaviour

I have been trying to access isMutating function over the queryClient provided by useQueryClient hook. Although whenever there is a mutation happening anywhere in the app, queryClient.isMutating() doesn't really return anything. But when we use useIsMutating hook for accessing if any mutation is happening it does return proper value. I am not sure why this is happening. Or maybe there is missing knowledge on my side. Please let me know Here is the codeSandbox link with minimal reproduction: https://codesandbox.io/p/sandbox/goofy-sun-l2wjth?file=%2Fsrc%2FApp.js
2 Replies
stormy-gold
stormy-gold2y ago
you can't really call direct getters on the queryClient, like .isMutating or .getQueryData and expect them to be reactive - they don't create a subscription. It's like calling store.getState() in redux - you need useSelector to make your component re-render, and just like that, you need a hook (useQuery or useIsMutating) to have a reactive subscription. useQueryClient just reads the stable query client from react context, it doesn't react to changes so - don't use imperative APIs when you want a reactive one 🙂
absent-sapphire
absent-sapphireOP2y ago
Thank you so much 👍 appreciate it

Did you find this page helpful?