hook for Background Fetching Loading States similar to useIsFetching #1062
Unanswered
FranciscoGileno
asked this question in
General
Replies: 1 comment 2 replies
-
I run into similar issue, using import { QueryClient, QueryFilters, notifyManager, useQueryClient } from "@tanstack/react-query";
import React from "react";
export function useIsBgFetching(
filters?: QueryFilters,
queryClient?: QueryClient,
): number {
const client = useQueryClient(queryClient)
const queryCache = client.getQueryCache()
return React.useSyncExternalStore(
React.useCallback(
(onStoreChange) =>
queryCache.subscribe(notifyManager.batchCalls(onStoreChange)),
[queryCache],
),
() => client.getQueryState(filters?.queryKey ?? [""])?.status !== "pending" ? client.isFetching(filters) : 0,
() => client.getQueryState(filters?.queryKey ?? [""])?.status !== "pending" ? client.isFetching(filters) : 0,
)
} Might be not the best or the most optimal solution, but works fine in my case, and may be useful for somebody. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Thanks for the awesome crazy lib.
I noticed there is a hook called
useIsFetching
which gives you the number of requests being fetched.I wonder if there is a way to have a simular hook only with the requests that are running in backend(Displaying Background Fetching Loading States).
I just had a look at the
useIsFetching
code, and it is pretty straight forward... I mean something like this:I can't do this on my project since
react-query
does not exportuseIsMounted
.Beta Was this translation helpful? Give feedback.
All reactions