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

InitialData.svelte

25 lines777 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: () => sleep(10).then(() => 'fetched'),
12 initialData: 'initial',
13 // make sure that initial data is older than the hydration data
14 // otherwise initialData would be newer and takes precedence
15 initialDataUpdatedAt: 1,
16 }))
17​
18 $effect(() => {
19 const snapshot = $state.snapshot(query)
20 states.current.push(snapshot)
21 })
22</script>
23​
24<div>{query.data}</div>
25<div>fetchStatus: {query.fetchStatus}</div>