You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using object destructuring assignment to extract 2 stores out of an object, like this:
letstore1letstore2({
store1,
store2,}=context);
, then only whichever store is assigned to a variable first (in this case store1) can be used. The other one is undefined when you (auto)subscribe to it ($store2).
To Reproduce
I have tried many variations to try to identify which factors are necessary and sufficient conditions for this strange behavior to occur. The 1 broken behavior and various variations that seem to work fine are summarized in this REPL repro.
It appears that this strange behavior — $store2 being undefined (see Broken.svelte) — only occurs iff all of these conditions are true:
We use destructuring assignment instead of regular assignment (see AssignWithoutDestructure1.svelte)
Reference $store1 at all (see OnlyReferenceStore2.svelte)
Use let instead of const (see Const.svelte)
Destructure/assign store1 first (see AssignStore2First.svelte)
Why exactly can I not use this combination of factors in my Svelte component? What is the rule that I am breaking, and where is it documented?
Expected behavior
I expect to be able to use let and destructuring assignment with store objects just the same as I can with any other type of value.
so I don't think there's anything wrong with my JavaScript. So what is so special about stores as values that would make it not work? And why does let vs. const make a difference here? Is the Svelte compiler doing something special with lets (even though I'm not using $: here) that is causing this strange behavior?
Severity
It seems medium severe to me. I can probably work around it (I will attempt that next), but it certainly diminishes my confidence in Svelte when:
a Svelte store mysteriously reports being undefined for no apparent reason
something that seems like standard JavaScript just doesn't work as expected and I have to come up with an arbitrarily different way to write it
Maybe I'm just doing something wrong, but I couldn't figure it out (after hours of turning this into a simple as possible repro), so I reported this as a bug until proven otherwise... :)
The text was updated successfully, but these errors were encountered:
We need to make sure both subscription functions are called after the destructuring assignment happens. So we can continue to use the destructuring in the output, probably what makes them most sense is to make two separate subscription calls afterwards.
Describe the bug
When using object destructuring assignment to extract 2 stores out of an object, like this:
, then only whichever store is assigned to a variable first (in this case
store1
) can be used. The other one is undefined when you (auto)subscribe to it ($store2
).To Reproduce
I have tried many variations to try to identify which factors are necessary and sufficient conditions for this strange behavior to occur. The 1 broken behavior and various variations that seem to work fine are summarized in this REPL repro.
It appears that this strange behavior —
$store2
being undefined (seeBroken.svelte
) — only occurs iff all of these conditions are true:AssignWithoutDestructure1.svelte
)$store1
at all (seeOnlyReferenceStore2.svelte
)let
instead ofconst
(seeConst.svelte
)store1
first (seeAssignStore2First.svelte
)Why exactly can I not use this combination of factors in my Svelte component? What is the rule that I am breaking, and where is it documented?
Expected behavior
I expect to be able to use
let
and destructuring assignment with store objects just the same as I can with any other type of value.This works just fine, for example:
so I don't think there's anything wrong with my JavaScript. So what is so special about stores as values that would make it not work? And why does
let
vs.const
make a difference here? Is the Svelte compiler doing something special withlet
s (even though I'm not using$:
here) that is causing this strange behavior?Severity
It seems medium severe to me. I can probably work around it (I will attempt that next), but it certainly diminishes my confidence in Svelte when:
undefined
for no apparent reasonMaybe I'm just doing something wrong, but I couldn't figure it out (after hours of turning this into a simple as possible repro), so I reported this as a bug until proven otherwise... :)
The text was updated successfully, but these errors were encountered: