-
-
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
Function changing state in class does not trigger reactivity #15098
Comments
You need to use |
Oh thanks. So if this function comes from a library and I have no way to know if it is reactive, I need to do this too... It seems hard, but wouldn't there be a way for the compiler to change all |
You should be doing |
Well even better then! That means you could always issue a warning when someone puts just foo.bar instead of |
No, because if you don't rely on |
However, I do agree that in dev Svelte could inject code into all class methods that warn you that a method was called with the wrong context. I know that this is a JavaScript "feature" and people "should know", but Svelte could also make the dx here better. @Conduitry thoughts? Are there legitimate reasons that you'd want to call a class method with a different |
A warning is not an error though. It's meant for best practices. I think if we can have strong a11y warnings, we can have strong warnings for dx too. |
I don't think this should be in class methods. I think it should be at binding sites: replace <button onclick={test.init}>Click this to set the above to true</button> with <button onclick={checkUnboundSubscriptions(test.init, loc)}>Click this to set the above to true</button> and function checkUnboundSubscriptions(fx, loc) {
const prev = getSubscriptions()
fx()
if (!getSubsriptions() = prev) {
issueWarning(`${loc} - a state susbbscription happened into an unbound class function. use event={() => fx()} instead of event={fx} to track reactive state. See also https://github.com/sveltejs/svelte/issues/15098#issuecomment-2610943014`)
}
} This is of course an overly simplified view, but I think this would save countless hours of developer time. |
Describe the bug
I can't seem to wrap my head around svelte's new reactivity system.
Below is a simple example, and I don't understand why it is not working. You just create a state class:
and import it in the other component.
But the UI doesn't update.
Reproduction
https://svelte.dev/playground/0d52f8525fbf4e4382eda285c426aec7?version=5.19.2
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: