MCPcopy Create free account
hub / github.com/TanStack/query / App

Function App

examples/preact/auto-refetching/src/App.tsx:6–95  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4import { todosMutations, todosQueries } from './services/todos/queries'
5
6export default function App() {
7 const queryClient = useQueryClient()
8 const [intervalMs, setIntervalMs] = useState(1000)
9 const [value, setValue] = useState('')
10
11 const { status, data, error, isFetching } = useQuery({
12 ...todosQueries.list(),
13 // Refetch the data every second
14 refetchInterval: intervalMs,
15 })
16
17 const addMutation = useMutation({
18 ...todosMutations.create(),
19 onSuccess: () =>
20 queryClient.invalidateQueries({ queryKey: todosQueries.all() }),
21 })
22
23 const clearMutation = useMutation({
24 ...todosMutations.deleteAll(),
25 onSuccess: () =>
26 queryClient.invalidateQueries({ queryKey: todosQueries.all() }),
27 })
28
29 if (status === 'pending') return <h1>Loading...</h1>
30 if (status === 'error') return <span>Error: {error.message}</span>
31
32 return (
33 <div>
34 <h1>Auto Refetch with refetchInterval set to {intervalMs}ms</h1>
35 <p>
36 This example is best experienced on your own machine, where you can open
37 multiple tabs to the same localhost server and see your changes
38 propagate between the two.
39 </p>
40 <label>
41 Query Interval speed (ms):{' '}
42 <input
43 value={intervalMs}
44 onInput={(ev) => setIntervalMs(Number(ev.currentTarget.value))}
45 type="number"
46 step="100"
47 />{' '}
48 <span
49 style={{
50 display: 'inline-block',
51 marginLeft: '.5rem',
52 width: 10,
53 height: 10,
54 background: isFetching ? 'green' : 'transparent',
55 transition: !isFetching ? 'all .3s ease' : 'none',
56 borderRadius: '100%',
57 transform: 'scale(2)',
58 }}
59 />
60 </label>
61 <h2>Todo List</h2>
62 <form
63 onSubmit={(event) => {

Callers

nothing calls this directly

Calls 5

useQueryClientFunction · 0.90
useQueryFunction · 0.90
useMutationFunction · 0.90
invalidateQueriesMethod · 0.45
mutateMethod · 0.45

Tested by

no test coverage detected