File: reactivity.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
Svelte
Version
v4
Search...
+ K
Menu
Getting Started
API Reference
ESLint
Examples
Framework
Svelte
Version
v4
Menu
Getting Started
API Reference
ESLint
Examples
Copy Markdown
Svelte uses a compiler to build your code which optimises rendering. By default, variables will run once, unless they are referenced in your markup. To make a different variable or function reactive, you need to use reactive declarations . This also applies to Svelte Query.
In 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.
svelte
<script lang="ts">
import { createQuery } from '@tanstack/svelte-query'
let intervalMs = 1000
const endpoint = 'http://localhost:5173/api/data'
const query = createQuery({
queryKey: ['refetch'],
queryFn: async () => await fetch(endpoint).then((r) => r.json()),
refetchInterval: intervalMs,
})
</script>
<input bind:value={intervalMs} type="number" />
<script lang="ts">
import { createQuery } from '@tanstack/svelte-query'
let intervalMs = 1000
const endpoint = 'http://localhost:5173/api/data'
const query = createQuery({
queryKey: ['refetch'],
queryFn: async () => await fetch(endpoint).then((r) => r.json()),
refetchInterval: intervalMs,
})
</script>
<input bind:value={intervalMs} type="number" />
To solve this, you can prefix the query with $: to tell the compiler it should be reactive.
svelte
<script lang="ts">
import { createQuery } from '@tanstack/svelte-query'
let intervalMs = 1000
const endpoint = 'http://localhost:5173/api/data'
$: query = createQuery({
queryKey: ['refetch'],
queryFn: async () => await fetch(endpoint).then((r) => r.json()),
refetchInterval: intervalMs,
})
</script>
<input bind:value={intervalMs} type="number" />
<script lang="ts">
import { createQuery } from '@tanstack/svelte-query'
let intervalMs = 1000
const endpoint = 'http://localhost:5173/api/data'
$: query = createQuery({
queryKey: ['refetch'],
queryFn: async () => await fetch(endpoint).then((r) => r.json()),
refetchInterval: intervalMs,
})
</script>
<input bind:value={intervalMs} type="number" />
[###### Want to Skip the Docs?
Query.gg - The Official React Query Course
\
âIf youâre serious about *really* understanding React Query, thereâs no better way than with query.ggââTanner Linsley
Learn More](https://query.gg/?s=tanstack)
You are currently reading v4 docs. Redirect to latest version?
Latest Hide
