You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/svelte/reactivity.md
+18-16Lines changed: 18 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,45 +3,47 @@ id: reactivity
3
3
title: Reactivity
4
4
---
5
5
6
-
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 be able to react to changes in options you need to use [stores](https://svelte.dev/docs/svelte-store).
6
+
Svelte uses a compiler to build your code which optimises rendering. By default, components 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/docs/svelte-store).
7
7
8
-
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.
8
+
In the below example, the `refetchInterval` option is set from the variable `intervalMs`, which is bound to the input field. However, as the query is not able to react to changes in `intervalMs`, `refetchInterval` will not change when the input value changes.
9
9
10
10
```markdown
11
-
<script>
11
+
<script lang="ts">
12
12
import { createQuery } from '@tanstack/svelte-query'
To 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.
28
+
To solve this, we can convert `intervalMs` into a writable store. The query options can then be turned into a derived store, which will be passed into the function with true reactivity.
29
29
30
30
```markdown
31
-
<script>
31
+
<script lang="ts">
32
+
import { derived, writable } from 'svelte/store'
32
33
import { createQuery } from '@tanstack/svelte-query'
33
-
import type { CreateQueryOptions } from '@tanstack/svelte-query'
0 commit comments