T
TanStack15mo ago
fair-rose

in tanstack query, how do I find if a mutation has already run once?

Same as title.
8 Replies
fair-rose
fair-roseOP15mo ago
or do i have to use something like onSuccess and update another state?
genetic-orange
genetic-orange15mo ago
useMutationState | TanStack Query React Docs
useMutationState is a hook that gives you access to all mutations in the MutationCache. You can pass filters to it to narrow down your mutations, and select to transform the mutation state. Example 1: Get all variables of all running mutations
fair-rose
fair-roseOP15mo ago
Yep it is. Another question: is it possible for a use query to disable itself? Use case: I deploy a background job on page load and then use query with a small refetchInterval to monitor job's status. When the job is completed, I want to disable the query
wise-white
wise-white15mo ago
you want refetchInterval to be a function
fair-rose
fair-roseOP15mo ago
What needs to be returned from the function to disable it? false?
wise-white
wise-white15mo ago
yes
fair-rose
fair-roseOP15mo ago
How do i extract data from Query type?
afraid-scarlet
afraid-scarlet15mo ago
I use a helper type for this:
export type UseQueryData<T extends (...args: any) => ReturnType<typeof useQuery>> = T extends (
...args: any
) => ReturnType<typeof useQuery<unknown, unknown, infer Data>>
? Data
: never;
export type UseQueryData<T extends (...args: any) => ReturnType<typeof useQuery>> = T extends (
...args: any
) => ReturnType<typeof useQuery<unknown, unknown, infer Data>>
? Data
: never;

Did you find this page helpful?