Why cannot I specify a mutation input with useQueryKey?
It's supported with queries, but for some reason not with mutations. Why is this the case?
14 Replies
The use-case is that I want global loading states for mutation with https://tanstack.com/query/v4/docs/react/reference/useIsMutating
useIsMutating | TanStack Query Docs
useIsMutating is an optional hook that returns the number of mutations that your application is fetching (useful for app-wide loading indicators).
`tsx
which I need a key for, hence the need for
useQueryKey
Mutations don’t typically have query keys, they’re not cached
This is a react-query thing
(I guess you get that though from the second link)
Do we not expose isMutating via trpc.useContext? Many things that need query keys we do expose this way
If not it might be because trpc.useMutation actually serves the same purpose in the context of trpc
useContext
only gives you the queries, so no luck therebut I ended up here
useIsMutating
with the predicate where I can access the variables
works, but no typesaftey 😦
I guess this could be wrapped to a custom TRPC wrapper of useIsMutating
where the type is narrowed based on the mutationKey
Yeah very possibly could be something we’ve missed
Feel free to open a GitHub issue
I used
useIsMutating
straight from react-query, so nothing missed 😄
but yep, this can be a feature requestiirc we have these in v11
Oh wait you wanna get the mutation key with input
Nah
that was the initial idea, but a custom
useIsMutating
with the mutation state typed in the predicate
would be a better solutionreact-query hard sets the Mutation types to any, so a change would be required there as well
I had something like this in mind:
but the
as
step done by trpc
anything specific I could check out?Hey, I need somthing you solved with useIsMutating but with isMutating client helper. I filed an issue https://github.com/trpc/trpc/issues/4889, do you mind bringing some more ideas/support there?