How to refetch data for a query when a mutation is performed in a different component?
To give a quick explanation of what I’m trying to do, I’m creating a system where a user can request to join a group and then the admins can accept or decline the request. I've tried following the docs for invalidating a single query but it seems to not be working in my case.
Component A is fetching the request status on the user side through a useQuery on one of my tRPC procedures.
Component A (checking request status on user side)
Component B performs a mutation (accept or decline) on the admin side from one of my tRPC procedures and I want it to immediately update the request status that the user sees in Component A after the admin accepts or declines.
Component B (declining the request on admin side)
7 Replies
this is what i do https://trpc.io/docs/client/react/useContext#invalidate-full-cache-on-every-mutation
useContext | tRPC
useContext is a hook that gives you access to helpers that let you manage the cached data of the queries you execute via @trpc/react-query. These helpers are actually thin wrappers around @tanstack/react-query's queryClient methods. If you want more in-depth information about options and usage patterns for useContext helpers than what we provide...
Thanks! I’m trying to use the useContext hook to access my queries and invalidate them in the code snippets above but it doesn’t seem to be working. I’ve tried changing my query cache time, stale time, removing the refetch function from it but still can’t get it to work properly
I am also running into this issue. I try to do any of the
.invalidate()
functions using the context, but it doesn't cause a rerender in the other components. I tried .refetch()
too.
alex / KATT 's solution fixed the issue, but I am using firebase, and a full refresh of the data is not cost effective for me.@icy.icicle Hi, did you find a solution? having the same issue.
@_typedef Not the most optimal solution, but check out what @alexkatt does: https://trpc.io/docs/client/react/useContext#invalidate-full-cache-on-every-mutation
I hate it, but it will refetch every query on the page every time there is a mutation
useContext | tRPC
useContext is a hook that gives you access to helpers that let you manage the cached data of the queries you execute via @trpc/react-query. These helpers are actually thin wrappers around @tanstack/react-query's queryClient methods. If you want more in-depth information about options and usage patterns for useContext helpers than what we provide...
i believe an idea solution would be to be able to pass an array of dependencies. Dependencies can query keys / routers
Yep this works but I really dont want to refetch all queries in the cache. Wait so your useContext doesnt work aswell?