|
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 = '/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=${encodeURIComponent(value)}`).then((r) => |
25 | 25 | r.json(), |
26 | 26 | ), |
27 | 27 | onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }), |
28 | | - }) |
| 28 | + })) |
29 | 29 |
|
30 | | - const clearMutation = createMutation({ |
| 30 | + const clearMutation = createMutation(() => ({ |
31 | 31 | mutationFn: () => fetch(`${endpoint}?clear=1`).then((r) => r.json()), |
32 | 32 | onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }), |
33 | | - }) |
| 33 | + })) |
34 | 34 | </script> |
35 | 35 |
|
36 | 36 | <h1>Auto Refetch with stale-time set to {intervalMs}ms</h1> |
|
51 | 51 | margin-left:.5rem; |
52 | 52 | width:.75rem; |
53 | 53 | height:.75rem; |
54 | | - background: {$todos.isFetching ? 'green' : 'transparent'}; |
55 | | - transition: {!$todos.isFetching ? 'all .3s ease' : 'none'}; |
| 54 | + background: {todos.isFetching ? 'green' : 'transparent'}; |
| 55 | + transition: {!todos.isFetching ? 'all .3s ease' : 'none'}; |
56 | 56 | border-radius: 100%; |
57 | 57 | transform: scale(1.5)" |
58 | 58 | ></span> |
59 | 59 | </div> |
60 | 60 | </label> |
61 | 61 | <h2>Todo List</h2> |
62 | 62 | <form |
63 | | - on:submit={(e) => { |
| 63 | + onsubmit={(e) => { |
64 | 64 | e.preventDefault() |
65 | 65 | e.stopPropagation() |
66 | | - $addMutation.mutate(value, { |
| 66 | + addMutation.mutate(value, { |
67 | 67 | onSuccess: () => (value = ''), |
68 | 68 | }) |
69 | 69 | }} |
70 | 70 | > |
71 | 71 | <input placeholder="enter something" bind:value /> |
72 | 72 | </form> |
73 | 73 |
|
74 | | -{#if $todos.isPending} |
| 74 | +{#if todos.isPending} |
75 | 75 | Loading... |
76 | 76 | {/if} |
77 | | -{#if $todos.error} |
| 77 | +{#if todos.error} |
78 | 78 | An error has occurred: |
79 | | - {$todos.error.message} |
| 79 | + {todos.error.message} |
80 | 80 | {/if} |
81 | | -{#if $todos.isSuccess} |
| 81 | +{#if todos.isSuccess} |
82 | 82 | <ul> |
83 | | - {#each $todos.data.items as item} |
| 83 | + {#each todos.data.items as item} |
84 | 84 | <li>{item}</li> |
85 | 85 | {/each} |
86 | 86 | </ul> |
87 | 87 | <div> |
88 | | - <button on:click={() => $clearMutation.mutate(undefined)}> |
89 | | - Clear All |
90 | | - </button> |
| 88 | + <button onclick={() => clearMutation.mutate(undefined)}> Clear All </button> |
91 | 89 | </div> |
92 | 90 | {/if} |
93 | | -{#if $todos.isFetching} |
| 91 | +{#if todos.isFetching} |
94 | 92 | <div style="color:darkgreen; font-weight:700"> |
95 | 93 | 'Background Updating...' : ' ' |
96 | 94 | </div> |
|
0 commit comments