Skip to content

Commit 9ba370f

Browse files
authored
docs: replace $state.frozen docs with $state.raw (#12812)
1 parent 9f17f76 commit 9ba370f

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

documentation/docs/03-runes/01-state.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
title: State
33
---
44

5-
- `$state` (.frozen)
6-
- `$derived` (.by)
7-
- using classes
8-
- getters/setters (what to do to keep reactivity "alive")
9-
- universal reactivity
10-
115
Svelte 5 uses _runes_, a powerful set of primitives for controlling reactivity inside your Svelte components and inside `.svelte.js` and `.svelte.ts` modules.
126

137
Runes are function-like symbols that provide instructions to the Svelte compiler. You don't need to import them from anywhere — when you use Svelte, they're part of the language. This page describes the runes that are concerned with managing state in your application.
@@ -68,12 +62,12 @@ Objects and arrays are made deeply reactive by wrapping them with [`Proxies`](ht
6862

6963
> Only POJOs (plain old JavaScript objects) are made deeply reactive. Reactivity will stop at class boundaries and leave those alone
7064
71-
## `$state.frozen`
65+
## `$state.raw`
7266

73-
State declared with `$state.frozen` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:
67+
State declared with `$state.raw` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:
7468

7569
```js
76-
let person = $state.frozen({
70+
let person = $state.raw({
7771
name: 'Heraclitus',
7872
age: 49
7973
});
@@ -88,11 +82,7 @@ person = {
8882
};
8983
```
9084

91-
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 frozen state can _contain_ reactive state (for example, a frozen array of reactive objects).
92-
93-
In development mode, the argument to `$state.frozen` will be shallowly frozen with `Object.freeze()`, to make it obvious if you accidentally mutate it.
94-
95-
> Objects and arrays passed to `$state.frozen` will have a `Symbol` property added to them to signal to Svelte that they are frozen. If you don't want this, pass in a clone of the object or array instead. The argument cannot be an existing state proxy created with `$state(...)`.
85+
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).
9686

9787
## `$state.snapshot`
9888

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ export function should_proxy(node, scope) {
256256
) {
257257
return false;
258258
}
259+
259260
if (node.type === 'Identifier' && scope !== null) {
260261
const binding = scope.get(node.name);
261-
// Let's see if the reference is something that can be proxied or frozen
262+
// Let's see if the reference is something that can be proxied
262263
if (
263264
binding !== null &&
264265
!binding.reassigned &&
@@ -271,6 +272,7 @@ export function should_proxy(node, scope) {
271272
return should_proxy(binding.initial, null);
272273
}
273274
}
275+
274276
return true;
275277
}
276278

sites/svelte-5-preview/src/lib/autocomplete.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function is_state(node) {
6868

6969
/**
7070
* Returns `true` if we're already in a valid call expression, e.g.
71-
* changing an existing `$state()` to `$state.frozen()`
71+
* changing an existing `$state()` to `$state.raw()`
7272
* @type {Test}
7373
*/
7474
function is_state_call(node) {
@@ -113,8 +113,8 @@ const runes = [
113113
{ snippet: '$derived.by', test: is_state_call },
114114
{ snippet: '$effect(() => {\n\t${}\n});', test: is_statement },
115115
{ snippet: '$effect.pre(() => {\n\t${}\n});', test: is_statement },
116-
{ snippet: '$state.frozen(${});', test: is_state },
117-
{ snippet: '$state.frozen', test: is_state_call },
116+
{ snippet: '$state.raw(${});', test: is_state },
117+
{ snippet: '$state.raw', test: is_state_call },
118118
{ snippet: '$bindable()', test: is_bindable },
119119
{ snippet: '$effect.root(() => {\n\t${}\n})' },
120120
{ snippet: '$state.snapshot(${})' },

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ Only plain objects and arrays [are made deeply reactive](/#H4sIAAAAAAAAE42QwWrDM
6464

6565
In non-runes mode, a `let` declaration is treated as reactive state if it is updated at some point. Unlike `$state(...)`, which works anywhere in your app, `let` only behaves this way at the top level of a component.
6666

67-
## `$state.frozen`
67+
## `$state.raw`
6868

69-
State declared with `$state.frozen` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:
69+
State declared with `$state.raw` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:
7070

7171
```diff
7272
<script>
7373
- let numbers = $state([1, 2, 3]);
74-
+ let numbers = $state.frozen([1, 2, 3]);
74+
+ let numbers = $state.raw([1, 2, 3]);
7575
</script>
7676

7777
-<button onclick={() => numbers.push(numbers.length + 1)}>
@@ -89,9 +89,7 @@ State declared with `$state.frozen` cannot be mutated; it can only be _reassigne
8989
</p>
9090
```
9191

92-
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 frozen state can _contain_ reactive state (for example, a frozen array of reactive objects).
93-
94-
> Objects and arrays passed to `$state.frozen` will be shallowly frozen using `Object.freeze()`. If you don't want this, pass in a clone of the object or array instead.
92+
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).
9593

9694
## `$state.snapshot`
9795

0 commit comments

Comments
 (0)