-
It would be nice to have a mutation version of |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 27 replies
-
what would you need it for? The main reason for |
Beta Was this translation helpful? Give feedback.
-
I have another use case for function someMutationOptions() {
return mutationOptions({
mutationFn: ...
});
}
type AdditionalOptions = Omit<
ReturnType<typeof someMutationOptions>,
"mutationFn"
>;
export default function useSomeMutation(
additionalOptions?: AdditionalOptions
) {
const options = someMutationOptions();
return useMutation({
...options,
...additionalOptions,
});
} In this case additionalOptions type will be inferred from the mutationFn and we won't have to type options object manually |
Beta Was this translation helpful? Give feedback.
-
I'm working on an e-commerce app where we want to show a state for mutation in progress on a certain item in a users cart and using import { updateItemQuantityMutationKey } from '@/shopify/updateItemQuantityMutationKey'
import { useQueryClient } from '@tanstack/react-query'
export const useIsModifyingLineItem = (id: string) => {
const queryClient = useQueryClient()
return (
queryClient.isMutating({
mutationKey: updateItemQuantityMutationKey,
predicate: (options) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Boolean(options.state.variables?.lines?.find((line: any) => line?.id === id))
},
}) > 0
)
} Although I think in this case I'm just going to hoist the state up slightly so I don't have to use this code. If I was able to narrow the type using something like |
Beta Was this translation helpful? Give feedback.
-
Tried the following and seems to work well for me. import type { DefaultError } from "@tanstack/query-core";
import type { UseMutationOptions } from "@tanstack/react-query";
export function mutationOptions<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
>(
options: UseMutationOptions<TData, TError, TVariables, TContext>,
): UseMutationOptions<TData, TError, TVariables, TContext> {
return options;
} |
Beta Was this translation helpful? Give feedback.
what would you need it for? The main reason for
queryOptions
is to have a way to share options betweenuseQuery
,useQueries
and things likequeryClient.prefetchQuery
. There's no equivalent for mutations where this would be needed