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
@@ -3,25 +3,7 @@ NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-doc
3
3
title: svelte/reactivity
4
4
---
5
5
6
-
Svelte provides reactive versions of various built-ins like `SvelteMap`, `SvelteSet` and `SvelteURL`. These can be imported from `svelte/reactivity` and used just like their native counterparts.
7
-
8
-
```svelte
9
-
<script>
10
-
import { SvelteURL } from 'svelte/reactivity';
11
-
12
-
const url = new SvelteURL('https://example.com/path');
13
-
</script>
14
-
15
-
<!-- changes to these... -->
16
-
<input bind:value={url.protocol} />
17
-
<input bind:value={url.hostname} />
18
-
<input bind:value={url.pathname} />
19
-
20
-
<hr />
21
-
22
-
<!-- will update `href` and vice versa -->
23
-
<input bind:value={url.href} />
24
-
```
6
+
Svelte provides reactive versions of various built-ins like [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) that can be used just like their native counterparts, as well as a handful of additional utilities for handling reactivity.
A reactive version of the built-in [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object.
75
+
Reading the date (whether with methods like `date.getTime()` or `date.toString()`, or via things like [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat))
76
+
in an [effect](/docs/svelte/$effect) or [derived](/docs/svelte/$derived)
77
+
will cause it to be re-evaluated when the value of the date changes.
78
+
79
+
```svelte
80
+
<script>
81
+
import { SvelteDate } from 'svelte/reactivity';
82
+
83
+
const date = new SvelteDate();
84
+
85
+
const formatter = new Intl.DateTimeFormat(undefined, {
A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.
125
+
Reading contents of the map (by iterating, or by reading `map.size` or calling `map.get(...)` or `map.has(...)` as in the [tic-tac-toe example](/playground/0b0ff4aa49c9443f9b47fe5203c78293) below) in an [effect](/docs/svelte/$effect) or [derived](/docs/svelte/$derived)
126
+
will cause it to be re-evaluated as necessary when the map is updated.
127
+
128
+
Note that values in a reactive map are _not_ made [deeply reactive](/docs/svelte/$state#Deep-state).
129
+
130
+
```svelte
131
+
<script>
132
+
import { SvelteMap } from 'svelte/reactivity';
133
+
import { result } from './game.js';
134
+
135
+
let board = new SvelteMap();
136
+
let player = $state('x');
137
+
let winner = $derived(result(board));
138
+
139
+
function reset() {
140
+
player = 'x';
141
+
board.clear();
142
+
}
143
+
</script>
144
+
145
+
<div class="board">
146
+
{#each Array(9), i}
147
+
<button
148
+
disabled={board.has(i) || winner}
149
+
onclick={() => {
150
+
board.set(i, player);
151
+
player = player === 'x' ? 'o' : 'x';
152
+
}}
153
+
>{board.get(i)}</button>
154
+
{/each}
155
+
</div>
156
+
157
+
{#if winner}
158
+
<p>{winner} wins!</p>
159
+
<button onclick={reset}>reset</button>
160
+
{:else}
161
+
<p>{player} is next</p>
162
+
{/if}
163
+
```
164
+
111
165
<divclass="ts-block">
112
166
113
167
```dts
@@ -136,6 +190,37 @@ set(key: K, value: V): this;
136
190
137
191
## SvelteSet
138
192
193
+
A reactive version of the built-in [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) object.
194
+
Reading contents of the set (by iterating, or by reading `set.size` or calling `set.has(...)` as in the [example](/playground/53438b51194b4882bcc18cddf9f96f15) below) in an [effect](/docs/svelte/$effect) or [derived](/docs/svelte/$derived)
195
+
will cause it to be re-evaluated as necessary when the set is updated.
196
+
197
+
Note that values in a reactive set are _not_ made [deeply reactive](/docs/svelte/$state#Deep-state).
const url = new SvelteURL('https://example.com/path');
265
+
</script>
266
+
267
+
<!-- changes to these... -->
268
+
<input bind:value={url.protocol} />
269
+
<input bind:value={url.hostname} />
270
+
<input bind:value={url.pathname} />
271
+
272
+
<hr />
273
+
274
+
<!-- will update `href` and vice versa -->
275
+
<input bind:value={url.href} size="65" />
276
+
```
277
+
167
278
<divclass="ts-block">
168
279
169
280
```dts
@@ -183,6 +294,31 @@ get searchParams(): SvelteURLSearchParams;
183
294
184
295
## SvelteURLSearchParams
185
296
297
+
A reactive version of the built-in [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) object.
298
+
Reading its contents (by iterating, or by calling `params.get(...)` or `params.getAll(...)` as in the [example](/playground/b3926c86c5384bab9f2cf993bc08c1c8) below) in an [effect](/docs/svelte/$effect) or [derived](/docs/svelte/$derived)
299
+
will cause it to be re-evaluated as necessary when the params are updated.
300
+
301
+
```svelte
302
+
<script>
303
+
import { SvelteURLSearchParams } from 'svelte/reactivity';
304
+
305
+
const params = new SvelteURLSearchParams('message=hello');
0 commit comments