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: documentation/docs/02-runes/02-$state.md
+6-4Lines changed: 6 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,7 @@ Unlike other frameworks you may have encountered, there is no API for interactin
20
20
21
21
If `$state` is used with an array or a simple object, the result is a deeply reactive _state proxy_. [Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) allow Svelte to run code when you read or write properties, including via methods like `array.push(...)`, triggering granular updates.
22
22
23
-
> [!NOTE] Class instances are not proxied. You can create [reactive state fields](#Classes) on classes that you define. Svelte provides reactive implementations of built-ins like `Set` and `Map` that can be imported from [`svelte/reactivity`](svelte-reactivity).
24
-
25
-
State is proxified recursively until Svelte finds something other than an array or simple object. In a case like this...
23
+
State is proxified recursively until Svelte finds something other than an array or simple object (like a class). In a case like this...
26
24
27
25
```js
28
26
F553
let todos =$state([
@@ -67,7 +65,7 @@ todos[0].done = !todos[0].done;
67
65
68
66
### Classes
69
67
70
-
You can also use `$state` in class fields (whether public or private), or as the first assignment to a property immediately inside the `constructor`:
68
+
Class instances are not proxied. Instead, you can use `$state` in class fields (whether public or private), or as the first assignment to a property immediately inside the `constructor`:
71
69
72
70
```js
73
71
// @errors: 7006 2554
@@ -121,6 +119,8 @@ class Todo {
121
119
}
122
120
```
123
121
122
+
> Svelte provides reactive implementations of built-in classes like `Set` and `Map` that can be imported from [`svelte/reactivity`](svelte-reactivity).
123
+
124
124
## `$state.raw`
125
125
126
126
In cases where you don't want objects and arrays to be deeply reactive you can use `$state.raw`.
@@ -145,6 +145,8 @@ person = {
145
145
146
146
This can improve performance with large arrays and objects that you weren't planning to mutate anyway, since it avoids the cost of making them reactive. Note that raw state can _contain_ reactive state (for example, a raw array of reactive objects).
147
147
148
+
As with `$state`, you can declare class fields using `$state.raw`.
149
+
148
150
## `$state.snapshot`
149
151
150
152
To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snapshot`:
0 commit comments