Skip to content

Commit 77434b2

Browse files
committed
docs: add docs on state_unsafe_mutation error
closes #14752
1 parent 7737868 commit 77434b2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

documentation/docs/98-reference/.generated/client-errors.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,27 @@ Reading state that was created inside the same derived is forbidden. Consider us
133133
```
134134
Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
135135
```
136+
137+
This error is thrown in a situation like this:
138+
139+
```svelte
140+
<script>
141+
let count = $state(0);
142+
let multiple = $derived.by(() => {
143+
const result = count * 2;
144+
if (result > 10) {
145+
count = 0;
146+
}
147+
return result;
148+
});
149+
</script>
150+
151+
<button onclick={() => count++}>{count} / {multiple}</button>
152+
```
153+
154+
Here, the `$derived` updates `count`, which is `$state` and therefore forbidden to do. It is forbidden because the reactive graph could become unstable as a result, leading to subtle bugs, like values being stale or effects firing in the wrong order. To prevent this, Svelte errors when detecting an update to a `$state` variable.
155+
156+
To fix this:
157+
- See if it's possible to refactor your `$derived` such that the update becomes unnecessary
158+
- Think about why you need to update `$state` inside a `$derived` in the first place. Maybe it's because you're using `bind:`, which leads you down a bad code path, and separating input and output path (by splitting it up to an attribute and an event, or by using [Function bindings](bind#Function-bindings)) makes it possible avoid the update
159+
- If it's unavoidable, you may need to use an [`$effect`]($effect) instead. This could include splitting parts of the `$derived` into an [`$effect`]($effect) which does the updates

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,27 @@ See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-long
8787
## state_unsafe_mutation
8888

8989
> Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
90+
91+
This error is thrown in a situation like this:
92+
93+
```svelte
94+
<script>
95+
let count = $state(0);
96+
let multiple = $derived.by(() => {
97+
const result = count * 2;
98+
if (result > 10) {
99+
count = 0;
100+
}
101+
return result;
102+
});
103+
</script>
104+
105+
<button onclick={() => count++}>{count} / {multiple}</button>
106+
```
107+
108+
Here, the `$derived` updates `count`, which is `$state` and therefore forbidden to do. It is forbidden because the reactive graph could become unstable as a result, leading to subtle bugs, like values being stale or effects firing in the wrong order. To prevent this, Svelte errors when detecting an update to a `$state` variable.
109+
110+
To fix this:
111+
- See if it's possible to refactor your `$derived` such that the update becomes unnecessary
112+
- Think about why you need to update `$state` inside a `$derived` in the first place. Maybe it's because you're using `bind:`, which leads you down a bad code path, and separating input and output path (by splitting it up to an attribute and an event, or by using [Function bindings](bind#Function-bindings)) makes it possible avoid the update
113+
- If it's unavoidable, you may need to use an [`$effect`]($effect) instead. This could include splitting parts of the `$derived` into an [`$effect`]($effect) which does the updates

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy