Skip to content

Commit cc5dc20

Browse files
committed
Expanded svelte/reactivity documentation
1 parent 999b92d commit cc5dc20

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

documentation/docs/98-reference/21-svelte-reactivity.md

+93
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,97 @@ Svelte provides reactive versions of various built-ins like `SvelteMap`, `Svelte
2222
<input bind:value={url.href} />
2323
```
2424

25+
The utilities provided in `svelte/reactivity` are automatically reactive with respect to their properties and methods, as seen in the previous example. As such, they don't need to be wrapped in `$state`. However, if a variable is reassigned, it needs to be wrapped in a `$state` in order for this reassignement to be reactive.
26+
27+
```svelte
28+
<script>
29+
import { SvelteURL } from 'svelte/reactivity';
30+
31+
let url = $state(new SvelteURL('https://example.com/path'));
32+
</script>
33+
34+
<!-- these are reactive... -->
35+
Protocol: {url?.protocol ?? "ftp:"}
36+
<br>
37+
Hostname: {url?.hostname ?? "svelte.dev"}
38+
<br>
39+
Path: {url?.pathname ?? ""}
40+
41+
<hr />
42+
43+
<!-- ...even when reassigning -->
44+
<button
45+
onclick={() => {
46+
url = undefined;
47+
}}
48+
>
49+
Erase
50+
</button>
51+
```
52+
53+
In a similar manner, the values stored inside e.g. `SvelteMap` are not automatically reactive, so if more complex values such as objects are used, they need to be wrapped in a `$state` in order to make their properties reactive as well. Alternatively, the whole object can be rewritten on update, which may actually lead to better performance than deep reactive `$state`.
54+
55+
```svelte
56+
<script>
57+
import { SvelteMap } from 'svelte/reactivity';
58+
59+
const people = new SvelteMap();
60+
61+
const alice = {name: "Alice", age: 18};
62+
people.set("alice", alice);
63+
64+
const bob = $state({name: "Bob", age: 21});
65+
people.set("bob", bob);
66+
</script>
67+
68+
{#each people.entries() as [id, person] (id)}
69+
Name: {person.name}
70+
<br>
71+
Age: {person.age}
72+
<br>
73+
<br>
74+
{/each}
75+
76+
<hr />
77+
78+
<!-- This will NOT propagate reactively -->
79+
<button
80+
onclick={() => {
81+
people.get("alice").age++;
82+
}}
83+
>
84+
Alice's birthday
85+
</button>
86+
87+
<!-- This WILL propagate reactively -->
88+
<button
89+
onclick={() => {
90+
people.get("bob").age++;
91+
}}
92+
>
93+
Bob's birthday
94+
</button>
95+
96+
<!-- This WILL propagate reactively -->
97+
<button
98+
onclick={() => {
99+
people.set("carol", {name: "Carol", age: 0});
100+
}}
101+
>
102+
Carol was born
103+
</button>
104+
105+
{#if people.has("carol")}
106+
<!-- This WILL propagate reactively -->
107+
<button
108+
onclick={() => {
109+
const oldValue = people.get("carol");
110+
people.set("carol", {...oldValue, age: oldValue.age + 1});
111+
}}
112+
>
113+
Carol's birthday
114+
</button>
115+
{/if}
116+
```
117+
25118
> MODULE: svelte/reactivity

0 commit comments

Comments
 (0)