-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Binding to store values not working correctly in Svelte 5 #11626
Comments
From my understanding, I'd say that you're indeed misusing stores, and this is how Svelte 5 fine-grained reactivity works. You're not changing the value of The reason that it works in Svelte 4, is not because of store updates, but because of the compiler invalidating the whole function input_change_handler() {
state.checked = this.checked;
$$invalidate(0, state);
} This makes In Svelte 5, the compiler no longer "guesses" what changed, instead moving that to run-time using signals. And since there are no signals on |
Well, stores are still a supported part of Svelte 5 and they just do not have fine-grained reactivity. A problem probably is that the Checkbox(node, {
get state() {
return $state();
},
set state($$value) {
$.store_set(state, $$value);
}
}); The setter is currently not invoked. Maybe some metadata could be passed along to make the |
This looks to be working as expected for runes mode. Svelte 4 doesn't have runes mode, so you're not comparing apples to apples. I wonder if you remove the usage of runes, does this issue go away? Then it will force the components into legacy mode which eagerly overreads because of the lack of fine grain reactivity. Legacy mode is also more aligned to how Svelte 4 works, so if there's a difference then that's likely a bug we need to fix. |
If the It will cause the $.bind_checked(
input,
() => state().checked,
($$value) => state((state().checked = $$value, state()))
); |
I'd say this works as expected in runes mode, though the behavior is probably a bit surprising at first. I'm wondering if there's a way to warn in this case (in dev mode). |
Describe the bug
I'm seeing issues with binding to values within stores in Svelte 5. Simple code that I trivially ported from Svelte 4 experiences issues with the the store not updating through a
bind
.I've attached two very small (~10 LOC) playground links that demo the issue. It's possible that perhaps I was mis-using stores or relying on non-guaranteed behavior in the past, but this functionality did indeed work in Svelte 4 and broke my app when I started porting over some components to use runes.
Here's the code for the broken playground inline for convenience:
App.svelte:
Checkbox.svelte:
Expected Behavior:
The value in the store is updated when the checkbox is toggled. The checkbox will switch from checked to unchecked and the text "Selected" will appear/dissappear. The
$inspect
runes will print out the values in both theCheckbox
andApp
components when it's toggled.Actual Behavior:
The checkbox does toggle between checked and unchecked when clicked.
However:
$inspect
runes don't print anything except on the initial renderThis leads me to believe that the store isn't getting updated, or if it is things aren't changing in a way that triggers reactive re-renders.
Reproduction
Working playground (Svlete 4)
Broken playground (Svlete 5)
Logs
No response
System Info
Severity
blocking an upgrade
The text was updated successfully, but these errors were encountered: