invalidate query
whenever i add a user my user list is not updating
does not get updated
what i have tried :
searched for invalidate query but couldn't know how to setup a mutation key and invalidate it
caused an error
9 Replies
You could call
refetch
after you've awaited your mutation.
const { refetch } = trpc.user.getUsers.useQuery()
thanks its working, actually i used to do invalidate query when using useQuery by setting a mutation key and invalidate my query whenever any update changes
The correct way is indeed to call invalidate() on your query(s)
you want useUtils: https://trpc.io/docs/client/react/useUtils
useUtils | tRPC
useUtils 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 h...
that's true , thanks Nick !
actually invalidating query is not updating my user table
That’s a sign something else is wrong, in this case possible onsuccess never fires because you navigate before it has a chance to finish
Invalidate is a promise, I’d recommend awaiting it and navigating right after
actually even after awaiting the invalidation, it's still not updated , i wonder if it has anything related to the query which is being invalidated (this one)
however using utils.user.getUsers.refetch() is working idk why and how
Invalidate doesn’t actually do the refetch, it tells RQ that the query is stale and then RQ will refresh in a new cycle
If it doesn’t work it implies some weird timing thing going on with your navigation (try commenting that out)
I’ve seen a few situations invalidation can break down but it’s always a legit bug in userland
actually i wanted to comment it out , but didnt know how am i going to check if the data , getting invalidated or not ,
i placed the same table in the same page and the problem get fixed
but i still want to navigate to the other page