Skip to content

Svelte 5: Sub-object losing reactivity #11069

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

Closed
enyo opened this issue Apr 5, 2024 · 3 comments · Fixed by #11102 or #11109
Closed

Svelte 5: Sub-object losing reactivity #11069

enyo opened this issue Apr 5, 2024 · 3 comments · Fixed by #11102 or #11109
Assignees
Milestone

Comments

@enyo
Copy link

enyo commented Apr 5, 2024

Describe the bug

I'm not sure if this is intended behavior, so I'm reporting to make sure.

When you destructure a $state object to a primitive value it loses reactivity. That's to be expected.

But I assumed, that I would be able to destructure a sub-object, and maintain proper reactivity when modifying this object.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE5WRTW-DMAyG_4oXTWorVXBPAWnaadddxw4thDYdJAibrVPEf18-oGVrVWmHHOzYfh-_NqyStUDG3wxT20Ywzp7alq0ZfbcuwE9Rk7Ax6r4rXCbBopMtZbnKSTat7ggMFFqROBEMUHW6gUUUj5koDIiOuNjMOp4Psi7PtT4aK0OZe3YCutkoiKTaox2eTkK5SuILh0p2PZFWoFVRy-IjNcsVpNm5M8KD_npRr_udnfBwnR38Mr4VzI1vpxYUglqgjzNrS6NLWUlRMk5dL4b12cXf-18MPeLcTHHydoRVJxNTeETaklgahzXxcPChTUxcHJymS1rCYXWPxhP_96ojz72bzu40cc6utPlzJk_Bb1p8Df8-_ABVkSoAnAIAAA==

Logs

No response

System Info

Repl

Severity

annoyance

@enyo enyo changed the title Sub-object losing reactivity Svelte 5: Sub-object losing reactivity Apr 5, 2024
@brunnerh
Copy link
Member

brunnerh commented Apr 5, 2024

Note that you only have reactivity in App because it's not in runes mode.
REPL with mode set

I think this might be as intended. The proxy is only applied at the root.

Generally when destructuring from something stateful, one should use $derived.

const { settings } = $derived(context);

This then also works for primitives:

const { settings: { showInRgb } } = $derived(context);

REPL

@Rich-Harris
Copy link
Member

This definitely looks like a bug. My hunch — can't check right now — is that it's caused by this effect_active() check:

(effect_active() || updating_derived) &&

We should probably just always run this logic, even if we're getting the value in a non-reactive context (i.e. during component init)

@Rich-Harris
Copy link
Member

#11102 is insufficient — we need to remove the check altogether, not replace it with current_effect !== null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants