Open History to see the last commit
1---
2id: reactivity
3title: Reactivity
4---
5β
6Svelte uses a compiler to build your code which optimises rendering. By default, variables will run once, unless they are referenced in your markup. To be able to react to changes in options you need to use [stores](https://svelte.dev/tutorial/writable-stores).
7β
8In the below example, the `refetchInterval` option is set from the variable `intervalMs`, which is edited by the input field. However, as the query is not told it should react to changes in `intervalMs`, `refetchInterval` will not change when the input value changes.
9β
10```markdown
11<script>
12 import { createQuery } from '@tanstack/svelte-query'
13β
14 let intervalMs = 1000
15β
16 const endpoint = 'http://localhost:5173/api/data'
17β
18 const query = createQuery({
19 queryKey: ['refetch'],
20 queryFn: async () => await fetch(endpoint).then((r) => r.json()),
21 refetchInterval: intervalMs,
22 })
23</script>
24β
25<input bind:value={intervalMs} type="number" />
26```
27β
28To solve this, create a store for the options and use it as input for the query. Update the options store when the value changes and the query will react to the change.
29β
30```markdown
31<script>
32 import { createQuery } from '@tanstack/svelte-query'
33β
34 const endpoint = 'http://localhost:5173/api/data'
35β
36 const queryOptions = writable({
37 queryKey: ['refetch'],
38 queryFn: async () => await fetch(endpoint).then((r) => r.json()),
39 refetchInterval: 1000,
40 })
41 const query = createQuery(queryOptions)
42β
43 function updateRefetchInterval(event) {
44 $queryOptions.refetchInterval = event.target.valueAsNumber
45 }
46</script>
47β
48<input type="number" on:input={updateRefetchInterval} />
49```