melodyclue
melodyclue17mo ago

Sometimes invalidate not work...

hello sometimes invalidate method does not work in onSuccess anyone same? using NextJS, pnpm
No description
18 Replies
Typedef
Typedef16mo ago
Im having this problem aswell, @melodyclue have you found a solution?
melodyclue
melodyclueOP16mo ago
well, The problem is solved by writing an invalidate directly after the asynchronous process, instead of "onSuccess". We do not know why "onSuccess" does not work. In some cases it works, so I am confused.
Typedef
Typedef16mo ago
are you still invalidating through useContext? but just after the async
melodyclue
melodyclueOP16mo ago
yes I use ``` const utils = trpc.useContext() ... onSuccess() { utils. .... .invalidate() }
Typedef
Typedef16mo ago
Ahh, in my case onSuccess works all the time, but the utils...invalidate() doesnt.
David
David15mo ago
How did you solve this? cc @melodyclue
melodyclue
melodyclueOP15mo ago
@David In fact, it has not been resolved. It seems that invalidating within onSuccess does not execute. However, there are some that are executed and I am confused. For now, directly in the function await utils.project.getProject.invalidate({ project_id: projectId }); in the function for example, it seems to be executed.
David
David15mo ago
Damn that is rough, thanks for the quick response! A follow up question, do you happen to have 2 QueryClientProviders in your tree?
melodyclue
melodyclueOP15mo ago
@David 🧐 no, I dont use QueryClientProviders. instead I use export default trpc.withTRPC(App)
David
David15mo ago
I figured, thanks for the info! It seems that doing this:
const queryClient = useQueryClient()

// onSuccess
await queryClient.invalidateQueries(getQueryKey(trpc.path.to.query))
const queryClient = useQueryClient()

// onSuccess
await queryClient.invalidateQueries(getQueryKey(trpc.path.to.query))
melodyclue
melodyclueOP15mo ago
@David so useQueryClient is the key to solve this problrem? I will try !
David
David15mo ago
Bit of a workaround
Typedef
Typedef15mo ago
I had 2 queryClient providers thats why mine wasnt working 👍🏽
melodyclue
melodyclueOP15mo ago
@David oh you are talking about v9. mine is v10 actually...
David
David15mo ago
Nope, talking about v10 but later realized that also is not consistent it seems
melodyclue
melodyclueOP15mo ago
@David I think await queryClient.invalidateQueries(getQueryKey(trpc.path.to.query)) is the way how it works on v9 🤔
David
David15mo ago
It's just one of the standard react-query ways to do it
melodyclue
melodyclueOP15mo ago
@David ok figured out! you're right David. And this issue (invalidate not works) is fully resolved !!! https://trpc.io/docs/client/react/getQueryKeytrpcold
getQueryKey | tRPC
We provide a getQueryKey helper that accepts a router or procedure so that you can easily provide the native function the correct query key.