Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

stable-query-client.md

63 lines1.5 KB

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 )
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 )
40```
41​
42```tsx
43const queryClient = new QueryClient()
44function App() {
45 return (
46 <QueryClientProvider client={queryClient}>
47 <Home />
48 </QueryClientProvider>
49 )
51```
52​
53```tsx
54async function App() {
55 const queryClient = new QueryClient()
56 await queryClient.query(options)
58```
59​
60## Attributes
61​
62- [x] βœ… Recommended
63- [x] πŸ”§ Fixable