States don't update after Post

I'm having to manually invalidate the cache after every post in order for the states to update with the new data, but I feel like this shouldn't be happening. Am I doing something wrong?
7 Replies
isaac_way
isaac_way•2y ago
that's expected, invalidate is what tells react query to refetch the data from the server (which will have the updated data). if you don't refetch your data is going to be whatever you fetched before the mutation actually occurred which wouldn't contain the changes from the mutation
deforestor
deforestorOP•2y ago
so this is a valid way to do this?
isaac_way
isaac_way•2y ago
this works but it's going to refetch all your queries under the repository router, if you wanted to do less redundant fetches you could invalidate the specific query instead of the entire router like:
apiContext.repository.getRepository.invalidate({id: repositoryId})
apiContext.repository.getRepository.invalidate({id: repositoryId})
this would invalidate just the query that got changed and only refetch that one
deforestor
deforestorOP•2y ago
yeah, I know that, but, in this case in specific, I do need to invalidate all the queries They all complement each other in a way well, good to know it's right, thank you!
isaac_way
isaac_way•2y ago
yeah then it sounds like it's the correct way, no prob!
julius
julius•2y ago
If you prefer you can blow out the entire cache on every mutation and then not care about it 😉 https://trpc.io/docs/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...
deforestor
deforestorOP•2y ago
woah thank you, that's very useful
Want results from more Discord servers?
Add your server