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

InitialData.svelte

26 lines728 Bytes

1<script lang="ts">
2 import { createQuery } from '@tanstack/svelte-query'
3 import { sleep } from '../utils.js'
4 import type { Writable } from 'svelte/store'
5 import type { StatusResult } from '../utils.js'
6​
7 export let states: Writable<Array<StatusResult<string>>>
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 $: states.update((prev) => [...prev, $query])
23</script>
24​
25<div>{$query.data}</div>
26<div>fetchStatus: {$query.fetchStatus}</div>