-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible regression? Reactivity on store members triggers infinite loop in svelte 5 (works in svelte 4) #14306
Comments
$effect(() => {
let unsub = s.subscribe((obj) => {
if (obj.foo === 'baz') {
obj.bar = 'some';
} else {
obj.bar = null;
}
});
return () => {
unsub();
};
}); Is there a better way to do this? |
$effect(() => {
if ($s.foo === 'baz') {
untrack(() => $s).bar = 'some';
} else {
untrack(() => $s).bar = null;
}
}) I got that idea from the code that Svelte generates in the original example: $.legacy_pre_effect(() => ($s()), () => {
if ($s().foo === 'baz') {
$.store_mutate(s, $.untrack($s).bar = 'some', $.untrack($s));
} else {
$.store_mutate(s, $.untrack($s).bar = null, $.untrack($s));
}
}); |
Oh wait, that doesn't actually have the same outcome 😄 . Maybe it gets you on the right track? What definitely works if wrapping the assignments in if-block to check if it's already null/'some' |
This works as expected. The better solution is probably to rework the code so that you do the additional update at the point where the store is initially updated (inside the event in case of the REPL) - not sure how applicable that is in your case. Either way, this works as expected, therefore closing. |
Describe the bug
When using a store for reactivity, where the store contains an object, svelte 4 allows a reactive block to set a store member based on the value of another store member, without triggering reactivity on the whole store.
In svelte 5, this appears to no longer be possible - assignments to
$store.foo
trigger reactivity on$store.bar
.Example: the svelte 4 version runs once, the svelte 5 version triggers an infinite loop, when the button is clicked.
I'm not sure how to work around this when dealing with libraries that use stores - I am not in control here, and need to update the value of one store member when another store member changes.
Reproduction
https://svelte.dev/playground/hello-world?version=5.2.0#H4sIAAAAAAAAE32QT0-EMBDFv8qkIQESQu8IGG_Ggx48Wg_8mZrG0pJ21nUl_e6mC6jRjadp5v2m780szHQTsordotYWjtbpETIcFeGYs4JJpdGz6mlhdJojFxus2Kdu5rn0b6gp9vrO46X-YA2hIc8qVvvBqZlaYQSpabaOYIGjU9T1GiGAdHaCdJ3knqzD9CqyGgk8NF9otkhrK0iltWkBfecqMAetQx5pIyipYIlVkJKQJb6U1kLTNJD23Uea76KgxJd956CB1NtpMxMUALXHC1Q02ZlYwmrHOSQoJQ6UZTk07TrJOfzrf9YvRzhLP1P8Zr-CrOT2CrkwNf--sanndrl7fLgvPTllXpQ8ZYnPQ83nVpi6PxBZA9YMWg2vzbJl3-P-DQ7X68mh2hYJoZ3sqOSp5utnLSsY4TuxitwBw3P4BNax-WxhAgAA
Logs
No response
System Info
Severity
blocking an upgrade
The text was updated successfully, but these errors were encountered: