|
5 | 5 | createMutation,
|
6 | 6 | } from '@tanstack/svelte-query'
|
7 | 7 |
|
8 |
| - let intervalMs = 1000 |
9 |
| - let value = '' |
| 8 | + let intervalMs = $state(1000) |
| 9 | + let value = $state<string>('') |
10 | 10 |
|
11 | 11 | const client = useQueryClient()
|
12 | 12 |
|
13 | 13 | const endpoint = 'http://localhost:5173/api/data'
|
14 | 14 |
|
15 |
| - $: todos = createQuery<{ items: string[] }>({ |
| 15 | + const todos = createQuery<{ items: string[] }>(() => ({ |
16 | 16 | queryKey: ['refetch'],
|
17 | 17 | queryFn: async () => await fetch(endpoint).then((r) => r.json()),
|
18 | 18 | // Refetch the data every second
|
19 | 19 | refetchInterval: intervalMs,
|
20 |
| - }) |
| 20 | + })) |
21 | 21 |
|
22 |
| - const addMutation = createMutation({ |
| 22 | + const addMutation = createMutation(() => ({ |
23 | 23 | mutationFn: (value: string) =>
|
24 | 24 | fetch(`${endpoint}?add=${value}`).then((r) => r.json()),
|
25 | 25 | onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }),
|
26 |
| - }) |
| 26 | + })) |
27 | 27 |
|
28 |
| - const clearMutation = createMutation({ |
| 28 | + const clearMutation = createMutation(() => ({ |
29 | 29 | mutationFn: () => fetch(`${endpoint}?clear=1`).then((r) => r.json()),
|
30 | 30 | onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }),
|
31 |
| - }) |
| 31 | + })) |
32 | 32 | </script>
|
33 | 33 |
|
34 | 34 | <h1>Auto Refetch with stale-time set to 1s</h1>
|
|
49 | 49 | margin-left:.5rem;
|
50 | 50 | width:.75rem;
|
51 | 51 | height:.75rem;
|
52 |
| - background: {$todos.isFetching ? 'green' : 'transparent'}; |
53 |
| - transition: {!$todos.isFetching ? 'all .3s ease' : 'none'}; |
| 52 | + background: {todos.isFetching ? 'green' : 'transparent'}; |
| 53 | + transition: {!todos.isFetching ? 'all .3s ease' : 'none'}; |
54 | 54 | border-radius: 100%;
|
55 | 55 | transform: scale(1.5)"
|
56 | 56 | ></span>
|
57 | 57 | </div>
|
58 | 58 | </label>
|
59 | 59 | <h2>Todo List</h2>
|
60 | 60 | <form
|
61 |
| - on:submit={(e) => { |
| 61 | + onsubmit={(e) => { |
62 | 62 | e.preventDefault()
|
63 | 63 | e.stopPropagation()
|
64 |
| - $addMutation.mutate(value, { |
| 64 | + addMutation.mutate(value, { |
65 | 65 | onSuccess: () => (value = ''),
|
66 | 66 | })
|
67 | 67 | }}
|
68 | 68 | >
|
69 | 69 | <input placeholder="enter something" bind:value />
|
70 | 70 | </form>
|
71 | 71 |
|
72 |
| -{#if $todos.isPending} |
| 72 | +{#if todos.isPending} |
73 | 73 | Loading...
|
74 | 74 | {/if}
|
75 |
| -{#if $todos.error} |
| 75 | +{#if todos.error} |
76 | 76 | An error has occurred:
|
77 |
| - {$todos.error.message} |
| 77 | + {todos.error.message} |
78 | 78 | {/if}
|
79 |
| -{#if $todos.isSuccess} |
| 79 | +{#if todos.isSuccess} |
80 | 80 | <ul>
|
81 |
| - {#each $todos.data.items as item} |
| 81 | + {#each todos.data.items as item} |
82 | 82 | <li>{item}</li>
|
83 | 83 | {/each}
|
84 | 84 | </ul>
|
85 | 85 | <div>
|
86 |
| - <button on:click={() => $clearMutation.mutate(undefined)}> |
87 |
| - Clear All |
88 |
| - </button> |
| 86 | + <button onclick={() => clearMutation.mutate(undefined)}> Clear All </button> |
89 | 87 | </div>
|
90 | 88 | {/if}
|
91 |
| -{#if $todos.isFetching} |
| 89 | +{#if todos.isFetching} |
92 | 90 | <div style="color:darkgreen; font-weight:700">
|
93 | 91 | 'Background Updating...' : ' '
|
94 | 92 | </div>
|
|
0 commit comments