Open History to see the last commit
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'