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

polling.md

75 lines1.3 KB

1---
2id: polling
3title: Polling
4ref: docs/framework/react/guides/polling.md
5replace: { '@tanstack/react-query': '@tanstack/solid-query' }
6---
7​
8[//]: # 'Example1'
9​
10```tsx
11useQuery(() => ({
12 queryKey: ['prices'],
13 queryFn: fetchPrices,
14 refetchInterval: 5 * 1000, // every 5 seconds
15}))
16```
17​
18[//]: # 'Example1'
19[//]: # 'Example2'
20​
21```tsx
22useQuery(() => ({
23 queryKey: ['job', jobId],
24 queryFn: () => fetchJobStatus(jobId),
25 refetchInterval: (query) => {
26 // Stop polling once the job finishes
27 if (query.state.data?.status === 'complete') return false
28 return 2 * 1000
29 },
30}))
31```
32​
33[//]: # 'Example2'
34[//]: # 'Example3'
35​
36```tsx
37useQuery(() => ({
38 queryKey: ['portfolio'],
39 queryFn: fetchPortfolio,
40 refetchInterval: 30 * 1000,
41 refetchIntervalInBackground: true,
42}))
43```
44​
45[//]: # 'Example3'
46[//]: # 'Example4'
47​
48```tsx
49useQuery(() => ({
50 queryKey: ['prices', tokenAddress],
51 queryFn: () => fetchPrice(tokenAddress),
52 refetchInterval: () => {
53 if (!tokenAddress || isPaused) return false
54 return 15 * 1000
55 },
56}))
57```
58​
59[//]: # 'Example4'
60[//]: # 'Example5'
61​
62```tsx
63useQuery(() => ({
64 queryKey: ['chainStatus'],
65 queryFn: fetchChainStatus,
66 refetchInterval: 10 * 1000,
67 networkMode: 'always',
68}))
69```
70​
71[//]: # 'Example5'
72[//]: # 'ReactNative'
73[//]: # 'ReactNative'
74[//]: # 'ReRenders'
75[//]: # 'ReRenders'