javascripttanstack

Tanstack Query - Check when data are changing inside a mutation


I have a mutation and I would like to know if there is a way to know that the data of installedBuckets are changing ? Like for example isLoading or something like that ?

mutation :

const useUninstallBucket = () => {
  const queryClient = useQueryClient();
  const { mutate, isLoading: isMutating } = useMutation({
    mutationFn: (bucketName: string) => uninstallBucket(bucketName),
    onSuccess(bucket) {
      queryClient.setQueryData(["availableBuckets"], (buckets: any) => [
        ...buckets,
        bucket,
      ]);
      queryClient.setQueryData(["installedBuckets"], (buckets: any) => [
        ...buckets.filter((b: string) => b !== bucket),
      ]);
    },
  });
  return { mutate, isMutating };
};

installedBuckets query :

const useInstalledBuckets = () => {
  const {
    isLoading,
    data: buckets,
    error,
  } = useQuery(["installedBuckets"], getInstalledBuckets);
  return { isLoading, buckets, error };
};

Solution

  • For anyone that could face the same problem as me, here is the solution I found :

    const isMutatingBuckets = useIsMutating({ mutationKey: ["installedBuckets"] });
    

    From the documentation