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

stable-query-client.md

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