T
TanStack12mo 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-roseOP12mo ago
or do i have to use something like onSuccess and update another state?
exotic-emerald
exotic-emerald12mo 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-roseOP12mo 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
typical-coral
typical-coral12mo ago
you want refetchInterval to be a function
fair-rose
fair-roseOP12mo ago
What needs to be returned from the function to disable it? false?
typical-coral
typical-coral12mo ago
yes
fair-rose
fair-roseOP12mo ago
How do i extract data from Query type?
metropolitan-bronze
metropolitan-bronze12mo 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?