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•16mo 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
deforestor•16mo ago
so this is a valid way to do this?
isaac_way
isaac_way•16mo 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
deforestor•16mo 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•16mo ago
yeah then it sounds like it's the correct way, no prob!
julius
julius•16mo 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
deforestor•16mo ago
woah thank you, that's very useful