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

InitialData.svelte

30 lines852 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 { states }: { states: StatelessRef<Array<StatusResult<string>>> } =
7 $props()
8​
9 const query = createQuery(() => ({
10 queryKey: ['test'],
11 queryFn: async () => {
12 await sleep(5)
13 return 'fetched'
14 },
15​
16 initialData: 'initial',
17 // make sure that initial data is older than the hydration data
18 // otherwise initialData would be newer and takes precedence
19 initialDataUpdatedAt: 1,
20 }))
21​
22 $effect(() => {
23 // svelte-ignore state_snapshot_uncloneable
24 const snapshot = $state.snapshot(query)
25 states.current.push(snapshot)
26 })
27</script>
28​
29<div>{query.data}</div>
30<div>fetchStatus: {query.fetchStatus}</div>