Open History to see the last commit
1---
2id: stable-query-client
3title: Stable Query Client
4---
5β
6The QueryClient contains the QueryCache, so you'd only want to create one instance of the QueryClient for the lifecycle of your application - _not_ a new instance on every render.
7β
8> Exception: It's allowed to create a new QueryClient inside an async Server Component, because the async function is only called once on the server.
9β
10## Rule Details
11β
12Examples of **incorrect** code for this rule:
13β
14```tsx
15/* eslint "@tanstack/query/stable-query-client": "error" */
16β
17function App() {
18 const queryClient = new QueryClient()
19 return (
20 <QueryClientProvider client={queryClient}>
21 <Home />
22 </QueryClientProvider>
23 )
24}
25```
26β
27β
28Examples of **correct** code for this rule:
29β
30```tsx
31function App() {
32 const [queryClient] = useState(() => new QueryClient())
33 return (
34 <QueryClientProvider client={queryClient}>
35 <Home />
36 </QueryClientProvider>
37 )
38}
39```
40β
41```tsx
42const queryClient = new QueryClient()
43function App() {
44 return (
45 <QueryClientProvider client={queryClient}>
46 <Home />
47 </QueryClientProvider>
48 )
49}
50```
51β
52```
53async function App() {
54 const queryClient = new QueryClient()
55 await queryClient.prefetchQuery(options)
56}
57```
58β
59## Attributes
60β
61- [x] β
Recommended
62- [x] π§ Fixable