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β
27Examples of **correct** code for this rule:
28β
29```tsx
30function App() {
31 const [queryClient] = useState(() => new QueryClient())
32 return (
33 <QueryClientProvider client={queryClient}>
34 <Home />
35 </QueryClientProvider>
36 )
37}
38```
39β
40```tsx
41const queryClient = new QueryClient()
42function App() {
43 return (
44 <QueryClientProvider client={queryClient}>
45 <Home />
46 </QueryClientProvider>
47 )
48}
49```
50β
51```
52async function App() {
53 const queryClient = new QueryClient()
54 await queryClient.prefetchQuery(options)
55}
56```
57β
58## Attributes
59β
60- [x] β
Recommended
61- [x] π§ Fixable