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

stable-query-client.md

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