Skip to content

Commit 59bc4db

Browse files
committed
chore: tweak $state.is warning docs a bit more
1 parent 5a07219 commit 59bc4db

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,26 @@
4242

4343
> Reactive `$state(...)` proxies and the values they proxy have different identities. Because of this, comparisons with `%operator%` will produce unexpected results. Consider using `$state.is(a, b)` instead%details%
4444
45-
`$state(...)` does create a proxy of the value it is passed. Therefore, the resulting object has a different identity and equality checks will always return `false`:
45+
`$state(...)` creates a [proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) of the value it is passed. The proxy and the value have different identities, meaning equality checks will always return `false`:
4646

4747
```svelte
4848
<script>
49-
let object = { foo: 'bar' };
50-
let state_object = $state(object);
51-
object === state_object; // always false
49+
let value = { foo: 'bar' };
50+
let proxy = $state(value);
51+
52+
value === proxy; // always false
5253
</script>
5354
```
5455

55-
Most of the time this will not be a problem in practise because it is very rare to keep the original value around to later compare it against a state value. In case it happens, Svelte will warn you about it in development mode and suggest to use `$state.is` instead, which is able to unwrap the proxy and compare the original values:
56+
In the rare case that you need to compare them, you can use `$state.is`, which unwraps proxies:
5657

5758
```svelte
5859
<script>
59-
let object = { foo: 'bar' };
60-
let state_object = $state(object);
61-
$state.is(object, state_object); // true
60+
let value = { foo: 'bar' };
61+
let proxy = $state(value);
62+
63+
$state.is(value, proxy); // true
6264
</script>
6365
```
66+
67+
During development, Svelte will warn you when comparing values with proxies.

0 commit comments

Comments
 (0)