Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

FreshData.svelte

31 lines717 Bytes

1<script lang="ts">
2 import { createQuery } from '@tanstack/svelte-query'
3 import { sleep } from '@tanstack/query-test-utils'
4 import type { StatelessRef, StatusResult } from '../utils.svelte.js'
5​
6 let {
7 states,
8 onFetch,
9 }: {
10 states: StatelessRef<Array<StatusResult<string>>>
11 onFetch: () => void
12 } = $props()
13​
14 const query = createQuery(() => ({
15 queryKey: ['test'],
16 queryFn: async () => {
17 await sleep(10)
18 onFetch()
19 return 'fetched'
20 },
21 staleTime: Infinity,
22 }))
23​
24 $effect(() => {
25 const snapshot = $state.snapshot(query)
26 states.current.push(snapshot)
27 })
28</script>
29​
30<div>data: {query.data ?? 'null'}</div>
31<div>fetchStatus: {query.fetchStatus}</div>