context.client undefined in react query V5
The docs indicate that the onMutate option in the useMutation hook should be a context item that contains the query client. That would then allow us to use something like context.client.setQueryDate without calling queryClient = useQueryClient().
For example, I would love to instead of the above do something like:
The docs say this should work but I get an error saying context.client does not exist.
https://tanstack.com/query/latest/docs/framework/react/guides/mutations
Mutations | TanStack Query React Docs
Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. For this purpose, TanStack Query exports a useMutation hook. Here's an example of a mutation t...
5 Replies
sunny-greenOP•2w ago
I don't think the answer to that is right. If you look at the v5 docs, it is very clear onMutateResult is the context that comes out of the onMutate function, not context. this is slightly confusing because it used to be context.
Here is the side effect documentation for v5, showing onMutateResult is diff than context:
and here it is from v4, showing that v4 context is the same as onMutateResult (I think), and v5 context is something completely different, supposedly enabling us to get the queryClient context.
equal-aqua•2w ago
onMutate
receives variables, mutation, context
so it's the 3rd param. I think we just got this wrong in the docsonMutate
should be context
. If it's not, please show a minimal, runnable reproductionadverse-sapphire•2w ago
it seems to be correctly typed as expected w/ the latest query version:
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgVwM4FMCyyYEMbAQB2cAvnAGZQQhwDkAAnkangMYDWA9FOrmzAC0AR2TooATzoAoaW2KtEcEDnzoAgqglE2ZOAF4UGbHgLEAFAmlwby1WaIAxIgC44uLTrjmAlAYB8iLwwyFAkAArUIMAYAHS8qBAANgBu6OYAjD6kADTWtsQmam4e2rrmAPo5cPJEMOgAHjB++oFWtra19U2xbEnA6HWxAOboMACKYpIAIvi45gDadHQAuj75NrnSpD4A3LJAA
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
adverse-sapphire•2w ago
though for
"@tanstack/react-query": "^5.67.2"
context doesn't exist in onMutate like OP said, so it's probably added in one of the newer versions