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/98-reference/.generated/client-errors.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,9 +89,47 @@ Effect cannot be created inside a `$derived` value that was not itself created i
89
89
### effect_update_depth_exceeded
90
90
91
91
```
92
-
Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops
92
+
Maximum update depth exceeded. This typically indicates that an effect reads and writes the same piece of state
93
93
```
94
94
95
+
If an effect updates some state that it also depends on, it will re-run, potentially in a loop:
96
+
97
+
```js
98
+
let count =$state(0);
99
+
100
+
$effect(() => {
101
+
// this both reads and writes `count`,
102
+
// so will run in an infinite loop
103
+
count +=1;
104
+
});
105
+
```
106
+
107
+
(Svelte intervenes before this can crash your browser tab.)
108
+
109
+
The same applies to array mutations, since these both read and write to the array:
110
+
111
+
```js
112
+
let array =$state([]);
113
+
114
+
$effect(() => {
115
+
array.push('hello');
116
+
});
117
+
```
118
+
119
+
Note that it's fine for an effect to re-run itself as long as it 'settles':
120
+
121
+
```js
122
+
let array = ['a', 'b', 'c'];
123
+
// ---cut---
124
+
$effect(() => {
125
+
// this is okay, because sorting an already-sorted array
126
+
// won't result in a mutation
127
+
array.sort();
128
+
});
129
+
```
130
+
131
+
Often when encountering this issue, the value in question shouldn't be state (for example, if you are pushing to a `logs` array in an effect, make `logs` a normal array rather than `$state([])`). In the rare cases where you really _do_ need to write to state in an effect — [which you should avoid]($effect#When-not-to-use-$effect) — you can read the state with [untrack](svelte#untrack) to avoid adding it as a dependency.
Copy file name to clipboardExpand all lines: packages/svelte/messages/client-errors/errors.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,45 @@ See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-long
60
60
61
61
## effect_update_depth_exceeded
62
62
63
-
> Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops
63
+
> Maximum update depth exceeded. This typically indicates that an effect reads and writes the same piece of state
64
+
65
+
If an effect updates some state that it also depends on, it will re-run, potentially in a loop:
66
+
67
+
```js
68
+
let count =$state(0);
69
+
70
+
$effect(() => {
71
+
// this both reads and writes `count`,
72
+
// so will run in an infinite loop
73
+
count +=1;
74
+
});
75
+
```
76
+
77
+
(Svelte intervenes before this can crash your browser tab.)
78
+
79
+
The same applies to array mutations, since these both read and write to the array:
80
+
81
+
```js
82
+
let array =$state([]);
83
+
84
+
$effect(() => {
85
+
array.push('hello');
86
+
});
87
+
```
88
+
89
+
Note that it's fine for an effect to re-run itself as long as it 'settles':
90
+
91
+
```js
92
+
let array = ['a', 'b', 'c'];
93
+
// ---cut---
94
+
$effect(() => {
95
+
// this is okay, because sorting an already-sorted array
96
+
// won't result in a mutation
97
+
array.sort();
98
+
});
99
+
```
100
+
101
+
Often when encountering this issue, the value in question shouldn't be state (for example, if you are pushing to a `logs` array in an effect, make `logs` a normal array rather than `$state([])`). In the rare cases where you really _do_ need to write to state in an effect — [which you should avoid]($effect#When-not-to-use-$effect) — you can read the state with [untrack](svelte#untrack) to avoid adding it as a dependency.
Copy file name to clipboardExpand all lines: packages/svelte/src/internal/client/errors.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -214,12 +214,12 @@ export function effect_pending_outside_reaction() {
214
214
}
215
215
216
216
/**
217
-
* Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops
217
+
* Maximum update depth exceeded. This typically indicates that an effect reads and writes the same piece of state
218
218
* @returns {never}
219
219
*/
220
220
exportfunctioneffect_update_depth_exceeded(){
221
221
if(DEV){
222
-
consterror=newError(`effect_update_depth_exceeded\nMaximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops\nhttps://svelte.dev/e/effect_update_depth_exceeded`);
222
+
consterror=newError(`effect_update_depth_exceeded\nMaximum update depth exceeded. This typically indicates that an effect reads and writes the same piece of state\nhttps://svelte.dev/e/effect_update_depth_exceeded`);
0 commit comments