useIsFetching
Description: A standalone hook (not a useFetch option) that returns true if any data fetching is currently in progress (globally or for a specific key).
- How To: Use
useIsFetchingto show global loading indicators or to monitor specific data requests.// Global loading indicatorfunction GlobalLoader() {const isFetching = useIsFetching();if (!isFetching) return null;return <div className="spinner">Loading data...</div>;}// Monitoring a specific fetch keyfunction DataStatus({ userId }) {const isFetchingUser = useIsFetching(['user', userId]);return <span>{isFetchingUser ? 'Refreshing user data...' : ''}</span>;} - Best Practice: Use
useIsFetchingto provide visual feedback for background data synchronization, especially when usingsuspense: falseor when monitoring refetches that don't trigger a full Suspense fallback.
Deep Dive: How isFetching works step by step
Example:
const isFetching = useIsFetching();
What happens step by step:
- A fetch operation starts
DataClientinitiates a fetch (viauseFetch,refetch, orexecute).- The key for the fetch is added to the internal
fetchesset. - A
'load'event is published (globally or for the specific key).
useIsFetching()is notified- The hook is subscribed to the
'load'event. - With a key: If a key was provided to the hook, it listens to updates for that specific key.
- Global (no key): If no key was provided, it listens to the global
'load'event. - The subscription callback is triggered.
- The hook is subscribed to the
- Hook re-renders
- The hook checks the status of the request.
- With a key: It returns
trueonly if that specific cache entry's status is'fetching'. - Global (no key): It returns
trueifclient.pendingFetches()istrue(at least one fetch is active in the system).
- Fetch completes
DataClientremoves the key from thefetchesset.- A final
'load'event is published.
- Hook re-renders again
- The hook now returns
false(unless other fetches are still active in the global case).
- The hook now returns
Key Difference from isMutating:
useIsFetching is highly optimized to track either the entire system's fetching state or a specific key's status, making it ideal for granular UI feedback during background updates.