Skip to content

Commit 1901237

Browse files
committed
Expanded svelte/reactivity documentation
1 parent 76ce303 commit 1901237

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

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

+96
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,100 @@ 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+
// A plain object
62+
const alice = {name: "Alice", age: 18};
63+
64+
// A reactive object
65+
const bob = $state({name: "Bob", age: 21});
66+
67+
people.set("alice", alice);
68+
people.set("bob", bob);
69+
</script>
70+
71+
{#each people.entries() as [id, person] (id)}
72+
Name: {person.name}
73+
<br>
74+
Age: {person.age}
75+
<br>
76+
<br>
77+
{/each}
78+
79+
<hr />
80+
81+
<!-- This will NOT propagate reactively, because alice is a plain object -->
82+
<button
83+
onclick={() => {
84+
people.get("alice").age++;
85+
}}
86+
>
87+
Alice's birthday
88+
</button>
89+
90+
<!-- This WILL propagate reactively, because bob is a reactive object -->
91+
<button
92+
onclick={() => {
93+
people.get("bob").age++;
94+
}}
95+
>
96+
Bob's birthday
97+
</button>
98+
99+
<!-- This WILL propagate reactively, because people is reactive -->
100+
<button
101+
onclick={() => {
102+
people.set("carol", {name: "Carol", age: 0});
103+
}}
104+
>
105+
Carol was born
106+
</button>
107+
108+
{#if people.has("carol")}
109+
<!-- This WILL propagate reactively, because we are replacing the whole carol object -->
110+
<button
111+
onclick={() => {
112+
const oldValue = people.get("carol");
113+
people.set("carol", {...oldValue, age: oldValue.age + 1});
114+
}}
115+
>
116+
Carol's birthday
117+
</button>
118+
{/if}
119+
```
120+
25121
> MODULE: svelte/reactivity

0 commit comments

Comments
 (0)