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β
12This rule checks function declarations, function expressions, and arrow functions used as React components or custom hooks. It also reports clients created directly in JSX. Automatic fixes are available when the client is assigned to a variable.
13β
14Examples of **incorrect** code for this rule:
15β
16```tsx
17/* eslint "@tanstack/query/stable-query-client": "error" */
18β
19function App() {
20 const queryClient = new QueryClient()
21 return (
22 <QueryClientProvider client={queryClient}>
23 <Home />
24 </QueryClientProvider>
25 )
26}
27```
28β
29Examples of **correct** code for this rule:
30β
31```tsx
32function App() {
33 const [queryClient] = useState(() => new QueryClient())
34 return (
35 <QueryClientProvider client={queryClient}>
36 <Home />
37 </QueryClientProvider>
38 )
39}
40```
41β
42```tsx
43const queryClient = new QueryClient()
44function App() {
45 return (
46 <QueryClientProvider client={queryClient}>
47 <Home />
48 </QueryClientProvider>
49 )
50}
51```
52β
53```tsx
54async function App() {
55 const queryClient = new QueryClient()
56 await queryClient.query(options)
57}
58```
59β
60## Attributes
61β
62- [x] β
Recommended
63- [x] π§ Fixable