Stan aplikacji
state/query-client-stable
The QueryClient is created once - in useState(() => new QueryClient()) or a module helper - never in a component's render.
poważnastatycznefe-data-fetching
Dlaczego
const queryClient = new QueryClient() in a component body builds a new, empty cache on every render: every query refetches, loading spinners flash, and a refetch that re-renders the provider can loop. A client at module level is the opposite problem on a server: one cache shared by every request, leaking one user's data to the next.
Poprawka
In a component, create it once: const [queryClient] = useState(() => new QueryClient({ defaultOptions: { queries: { staleTime: 60_000 } } })). In a framework with server rendering, use a getQueryClient() helper that makes a new client per request on the server and reuses one in the browser. A plain client-only app may keep one module-level client.
Przykłady
// bad
export function Providers({ children }) {
const queryClient = new QueryClient()
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
}
// good
export function Providers({ children }) {
const [queryClient] = useState(() => new QueryClient())
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
}Źródła
Fakty
- Identyfikator
state/query-client-stable- Waga
- poważna
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
jsx- Tylko w projektach z
tanstack-query- Uczy jej skill
fe-data-fetchingData fetching- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain state/query-client-stable