Skip to content

Commit 9cd600d

Browse files
authored
Merge pull request #159 from kyle-leonhard/nested-effect-updates
Avoid triggering nested updates in $effects
2 parents 82b7278 + be39b67 commit 9cd600d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

.changeset/cuddly-poems-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-sonner": patch
3+
---
4+
5+
Avoid triggering nested $effect updates on dismissal

src/lib/Toast.svelte

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@
186186
187187
initialHeight = finalHeight;
188188
189-
toastState.setHeight({ toastId: toast.id, height: finalHeight });
189+
// setHeight reads heights and toasts state. Untrack the call
190+
// to avoid triggering this effect when those are modified. e.g. toasts
191+
// added and removed.
192+
untrack(() => {
193+
toastState.setHeight({ toastId: toast.id, height: finalHeight });
194+
});
190195
});
191196
192197
function deleteToast() {
@@ -265,7 +270,12 @@
265270
266271
$effect(() => {
267272
if (toast.delete) {
268-
deleteToast();
273+
// deleteToast reads and writes the heights and toasts state.
274+
// Untrack the call to avoid triggering nested updates.
275+
// See https://github.com/wobsoriano/svelte-sonner/issues/151
276+
untrack(() => {
277+
deleteToast();
278+
});
269279
}
270280
});
271281

0 commit comments

Comments
 (0)