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

Function Example

examples/solid/auto-refetching-solid-start2/src/routes/index.tsx:5–93  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3import { todosMutations, todosQueries } from '~/services/todos/queries'
4
5export default function Example() {
6 const queryClient = useQueryClient()
7 const [intervalMs, setIntervalMs] = createSignal(1000)
8 const [value, setValue] = createSignal('')
9
10 const todosQuery = useQuery(() => ({
11 ...todosQueries.list(),
12 // Refetch the data every second
13 refetchInterval: intervalMs(),
14 throwOnError: true,
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 return (
30 <ErrorBoundary fallback={(error) => <span>Error: {error.message}</span>}>
31 <Suspense fallback={<h1>Loading...</h1>}>
32 <div>
33 <h1>Auto Refetch with refetchInterval set to {intervalMs()}ms</h1>
34 <p>
35 This example is best experienced on your own machine, where you can
36 open multiple tabs to the same localhost server and see your changes
37 propagate between the two.
38 </p>
39 <label>
40 Query Interval speed (ms):{' '}
41 <input
42 value={intervalMs()}
43 onInput={(ev) => setIntervalMs(Number(ev.currentTarget.value))}
44 type="number"
45 step="100"
46 />{' '}
47 <span
48 style={{
49 display: 'inline-block',
50 'margin-left': '.5rem',
51 width: '10px',
52 height: '10px',
53 background: todosQuery.isFetching ? 'green' : 'transparent',
54 transition: !todosQuery.isFetching ? 'all .3s ease' : 'none',
55 'border-radius': '100%',
56 transform: 'scale(2)',
57 }}
58 />
59 </label>
60 <h2>Todo List</h2>
61 <form
62 onSubmit={(event) => {

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected