-
-
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
Reactive variable not updating as expected when assigned to store value #8211
Comments
You break the dependency analysis by using Change $: s.set(clickCount) to $: $s = clickCount and Svelte will order the statements correctly. Original compiled update order: $$self.$$.update = () => {
if ($$self.$$.dirty & /*$s*/ 1) {
// defining these two statments are defined before the set statement it results in weird reactivity behavior, but defined at the bottom of this script, it works correctly.
// try commenting these two lines and uncommenting the corresponding lines below
$: console.log($s);
}
if ($$self.$$.dirty & /*$s*/ 1) {
$: $$invalidate(1, directReaction = $s);
}
if ($$self.$$.dirty & /*clickCount*/ 16) {
$: s.set(clickCount);
}
}; After change: $$self.$$.update = () => {
if ($$self.$$.dirty & /*clickCount*/ 16) {
$: set_store_value(s, $s = clickCount, $s);
}
if ($$self.$$.dirty & /*$s*/ 1) {
// defining these two statments are defined before the set statement it results in weird reactivity behavior, but defined at the bottom of this script, it works correctly.
// try commenting these two lines and uncommenting the corresponding lines below
$: console.log($s);
}
if ($$self.$$.dirty & /*$s*/ 1) {
$: $$invalidate(1, directReaction = $s);
}
}; The order can also just manually be fixed in cases where the topological ordering is not possible, so this would work too: $: s.set(clickCount)
$: directReaction = $s |
Interesting. I would have guessed $s = clickCount is just syntactic sugar for s.set(clickCount). So, is there any reason one should ever use Store.set() vs $store = x? Thanks for the info! |
Outside of Svelte files you can't use the special syntax e.g. when implementing custom stores. |
Re-opening this issue because I found an example where your suggestion doesn't solve the issue. Namely, this same behavior isn't fixed by using the https://svelte.dev/repl/461f1b59c3e74177a1dcaa3371933bb8?version=3.57.0 |
This comes down to reactive statement ordering + rerun behavior. Svelte 5 will fix this. Closing as duplicate of #6732 |
Describe the bug
Reactive variables assigned to the value of a store do not correctly update if the reactive declaration is defined in the code before the store value itself is changed. The store value itself does change, but reactivity is never triggered for any variable reacting to changes in the store value. The strangest thing is that the where the directly reactive statement is defined changes the semantics of the code. Reproducing this bug is very simple, see the attached REPL.
Reproduction
https://svelte.dev/repl/8aecf4ef60f345fcacf6de3880beb7d1?version=3.35.0
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: