-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Description
Describe the bug
Hi Svelte Team,
I hope you're doing well! I’ve encountered an issue with reactivity in Svelte related to asynchronous updates, specifically with count2 not triggering as expected. I’m not sure if this is a known behavior or a bug, so I wanted to share the details.
Here’s the code I’m working with:
<script>
let count = $state(0);
let count2 = $state(0);
function increment() {
count += 1;
}
$effect(async() => {
await new Promise((res) => setTimeout(res, 1000));
count2 = count - 1
});
</script>
<button onclick={increment}>ok</button>
<div>{count}</div>
<div>{count2}</div>Issue:
When increment is called, count updates correctly after the delay, and the UI reflects the new value.
However, the $effect block, which depends on count, doesn’t seem to trigger reactivity for count2. The value of count2 doesn’t update in the UI, even though count has changed.
Expected Behavior:
The $effect block should re-run whenever count changes, and count2 should update reactively in the UI.
Is this a known limitation, or is there something I’m missing in how Svelte handles asynchronous reactivity in $effect? Any insights or suggestions would be greatly appreciated!
Thank you for your time and for all the incredible work on Svelte!
Reproduction
<script>
let count = $state(0);
let count2 = $state(0);
function increment() {
count += 1;
}
$effect(async() => {
await new Promise((res) => setTimeout(res, 1000));
count2 = count - 1
});
</script>
<button onclick={increment}>ok</button>
<div>{count}</div>
<div>{count2}</div>Logs
System Info
https://svelte.dev/tutorial/svelte/welcome-to-svelteSeverity
annoyance