Proposal for queryClient.isPaused feature #6756
-
I'm working on an offline-first React Native app using TanStack Query. To do this, I'd like to have a banner at the top of the screen indicating when the user needs to sync with the server (using paused queries to do this) This top banner should ideally hook into the const numberOfPaused = useIsPaused();
if (!numberOfPaused) return null;
return <p>You have {numberOfPaused} unsynced changed</p>; While we could hand-roll a 'use client'
import * as React from 'react'
import { notifyManager } from '@tanstack/query-core'
import { useQueryClient } from './QueryClientProvider'
import type { QueryClient, QueryFilters } from '@tanstack/query-core'
export function useIsPaused(
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],
),
() => queryCache.findAll({...filters, fetchStatus: 'paused' }),
() => queryCache.findAll({...filters, fetchStatus: 'paused' }),
)
} To handle this, a few thoughts:
I'm happy to introduce the relevant |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The hook won't exactly work because query/packages/react-query/src/useMutationState.ts Lines 49 to 82 in 1d3ee67
on the other hand, you could also simply hook into the so, I'm mostly concerned about api surface, but I'm even thinking that
which isn't that much more and I don't think those hooks are widely used. |
Beta Was this translation helpful? Give feedback.
yeah, let's do this. There might some things you can use from the
useMutationState
hook, and maybe we can even unify some of the internals with shared code. They should be pretty much the same except that one subscribes to queryCache, the other to mutationCache, and they have differently typed filters :)