-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Binding in slotted compoment triggers reactive statements twice #5720
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
Comments
This is known complex types binding behavior. Workarounds Binding arrays: use immutable in child component like this. Binding objects fields: $: doSomething(obj.foo);
let prevFoo;
$: if (obj.foo !== prevFoo ) {
prevFoo = obj.foo;
doSomething(obj.foo);
} or: $: foo = obj.foo;
$: doSomething(foo); |
I don't think this is true in this case. In the original repl, not using the slot by removing |
This Stack Overflow Question Why Svelte component updated variable, where in reality it didn't changed? |
It is VERY much a slot thing, replicated in https://svelte.dev/repl/2634d07aefa9465ea4ea7ec4bf810e8c?version=3.44.1 remove the slot and the behaviour vanishes. |
You are seeing this behavior because reactivity for objects (arrays) works fundamentally different for objects than for primitive types. |
|
Testing the original REPL with the latest of version of Svelte and the issue remains: I'm not seeing |
@zqianem Sorry, I changed x to a number, and it worked. It seems like only arrays and objects trigger double update, while numbers like Here's with |
Related issuue: #3617 |
The current architecture of Svelte propagates redraw through components without checking if has been updated. So if you have two path in the dependency graph.
One way to solve this is to send the origin component id and component event id together, like |
This issue seems to have been partially fixed by #7981. Now, the initial page load in the reproduction only prints "{}" once instead of twice. However, button presses still cause two prints. Compare the following: Original: https://svelte.dev/repl/df6fe28c60e84c26a141debf305ed114?version=3.30.0 |
This will be fixed in Svelte 5 - but also, slots will be deprecated in favor of the more powerful and easier to use snippets |
Describe the bug
When a variable is bound to a slotted component, changing that variable within the slotted component causes any reactive statements inside the slotted component to trigger twice.
To Reproduce
https://svelte.dev/repl/df6fe28c60e84c26a141debf305ed114?version=3.30.0
Expected behavior
Pressing the "Update x" button should cause only one
console.log
call, which is what happens when the<Wrapper>
is removed.Severity
Low — can work around this by using a store instead of binding.
The text was updated successfully, but these errors were encountered: