Skip to content

Commit 0e9d00e

Browse files
committed
docs: add derived proxy state (refer to #16189 for context)
1 parent 30e2b23 commit 0e9d00e

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

documentation/docs/02-runes/03-$derived.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,35 @@ Derived expressions are recalculated when their dependencies change, but you can
8585

8686
Unlike `$state`, which converts objects and arrays to [deeply reactive proxies]($state#Deep-state), `$derived` values are left as-is. For example, [in a case like this](/playground/untitled#H4sIAAAAAAAAE4VU22rjMBD9lUHd3aaQi9PdstS1A3t5XvpQ2Ic4D7I1iUUV2UjjNMX431eS7TRdSosxgjMzZ45mjt0yzffIYibvy0ojFJWqDKCQVBk2ZVup0LJ43TJ6rn2aBxw-FP2o67k9oCKP5dziW3hRaUJNjoYltjCyplWmM1JIIAn3FlL4ZIkTTtYez6jtj4w8WwyXv9GiIXiQxLVs9pfTMR7EuoSLIuLFbX7Z4930bZo_nBrD1bs834tlfvsBz9_SyX6PZXu9XaL4gOWn4sXjeyzftv4ZWfyxubpzxzg6LfD4MrooxELEosKCUPigQCMPKCZh0OtQE1iSxcsmdHuBvCiHZXALLXiN08EL3RRkaJ_kDVGle0HcSD5TPEeVtj67O4Nrg9aiSNtBY5oODJkrL5QsHtN2cgXp6nSJMWzpWWGasdlsGEMbzi5jPr5KFr0Ep7pdeM2-TCelCddIhDxAobi1jqF3cMaC1RKp64bAW9iFAmXGIHfd4wNXDabtOLN53w8W53VvJoZLh7xk4Rr3CoL-UNoLhWHrT1JQGcM17u96oES5K-kc2XOzkzqGCKL5De79OUTyyrg1zgwXsrEx3ESfx4Bz0M5UjVMHB24mw9SuXtXFoN13fYKOM1tyUT3FbvbWmSWCZX2Er-41u5xPoml45svRahl9Wb9aasbINJixDZwcPTbyTLZSUsAvrg_cPuCR7s782_WU8343Y72Qtlb8OYatwuOQvuN13M_hJKNfxann1v1U_B1KZ_D_mzhzhz24fw85CSz2irtN9w9HshBK7AQAAA==)...
8787

88-
```svelte
88+
```ts
8989
let items = $state([...]);
9090

9191
let index = $state(0);
9292
let selected = $derived(items[index]);
9393
```
9494

95-
...you can change (or `bind:` to) properties of `selected` and it will affect the underlying `items` array. If `items` was _not_ deeply reactive, mutating `selected` would have no effect.
95+
...you can change (or `bind:` to) properties of `selected` and it will affect the underlying `items` array.
96+
97+
If `items` was _not_ deeply reactive, mutating `selected` would have no effect. In that case, you need to make the derived variable reactive on its own.
98+
99+
```ts
100+
let items = [...];
101+
102+
let index = $state(0);
103+
104+
let selected = $derived.by(() => {
105+
const proxied = $state(items[index]);
106+
return proxied;
107+
});
108+
// ... or,
109+
function proxify<T>(init: T) {
110+
const proxied = $state(init);
111+
return proxied;
112+
}
113+
let selected = $derived(proxify(items[index]));
114+
```
115+
116+
> [!NOTE] This workaround detaches the state from the source, so mutating the proxied object won't update the source.
96117
97118
## Destructuring
98119

0 commit comments

Comments
 (0)