diff --git a/platforms/market/src/app/app-shell/hooks/use-slow-reads.ts b/platforms/market/src/app/app-shell/hooks/use-slow-reads.ts index 95048c5..fc78ff8 100644 --- a/platforms/market/src/app/app-shell/hooks/use-slow-reads.ts +++ b/platforms/market/src/app/app-shell/hooks/use-slow-reads.ts @@ -1,6 +1,6 @@ 'use client'; -import type { QueryClient } from '@tanstack/react-query'; +import { notifyManager, type QueryClient } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import { SLOW_READ_MS } from '../constants'; @@ -18,6 +18,7 @@ export function useSlowReads(queryClient: QueryClient): boolean { // When each first read still loading started, by query. const started = new Map(); let timer: ReturnType | undefined; + let active = true; const check = () => { clearTimeout(timer); @@ -43,9 +44,16 @@ export function useSlowReads(queryClient: QueryClient): boolean { if (wait > 0) timer = setTimeout(check, wait); }; - const unsubscribe = cache.subscribe(check); + // The cache notifies while a child's useQuery builds its query mid-render; defer so this + // component's setState never lands inside another component's render. + const unsubscribe = cache.subscribe(() => + notifyManager.schedule(() => { + if (active) check(); + }), + ); check(); return () => { + active = false; unsubscribe(); clearTimeout(timer); };