Typedef
Typedefā€¢12mo ago

useContext not Invalidating

What are the possible mistakes if useContext wont invalidate when doing the following:
const utils = trpc.useContext();
const user = trpc.user.getUser.useQuery();
const updateUser = trpc.user.updateUser.useMutation({
onSuccess: () => {
utils.user.getUser.invalidate();
}
});
const utils = trpc.useContext();
const user = trpc.user.getUser.useQuery();
const updateUser = trpc.user.updateUser.useMutation({
onSuccess: () => {
utils.user.getUser.invalidate();
}
});
Used to work before but stopped working when I turned my entire repo to turborepo.
8 Replies
Alex / KATT šŸ±
Alex / KATT šŸ±ā€¢12mo ago
could be that you should await the invalidate to the mutation stays loading until invalidation is complete
const updateUser = trpc.user.updateUser.useMutation({
onSuccess: async () => {
await utils.user.getUser.invalidate();
}
});
const updateUser = trpc.user.updateUser.useMutation({
onSuccess: async () => {
await utils.user.getUser.invalidate();
}
});
`
Typedef
Typedefā€¢11mo ago
Still doesnt work hmm but if I do user.refetch() it works I can confirm that it calls onSuccess after but not sending a query at all Any ideas @alexkatt Still not working ..
Alex / KATT šŸ±
Alex / KATT šŸ±ā€¢11mo ago
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...
Typedef
Typedefā€¢11mo ago
Hmm, I really dont want to invalidate eveything on any onSuccess mutations Any ideas on why it doesnt work?
Alex / KATT šŸ±
Alex / KATT šŸ±ā€¢11mo ago
Idk maybe you have to react query providers or something
Typedef
Typedefā€¢11mo ago
im using nextjs, my app is wrapped with withTrpc() Okay weird, I tried doing an override:
overrides: {
useMutation: {
async onSuccess(opts) {
await opts.originalFn();
await opts.queryClient.invalidateQueries();
},
},
},
overrides: {
useMutation: {
async onSuccess(opts) {
await opts.originalFn();
await opts.queryClient.invalidateQueries();
},
},
},
it invalidated all the cached queries. Still lost why it wont invalidte with const utils = trpc.useContext();
RealityShift
RealityShiftā€¢6mo ago
@Typedef did yoy ever find a solution to this? I'm experiencing the same issue.
Typedef
Typedefā€¢6mo ago
Yep check if you have another library using tanstack react query