Skip to content
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

$state looses reactivity with destructuring #11911

Closed
adamjkb opened this issue Jun 5, 2024 · 3 comments
Closed

$state looses reactivity with destructuring #11911

adamjkb opened this issue Jun 5, 2024 · 3 comments

Comments

@adamjkb
Copy link

adamjkb commented Jun 5, 2024

Describe the bug

Using destructuring assignment when $state rune is returned in a getter.

- const counter = createCounter();
+ let { count, increment } = createCounter();

The above result in different behaviour in template and $effect as well.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE32R0U7DMAxFf8WKkLaJqoPXsiGh8c4HMB5G5o6M1qkSF4Gq_DtOunaMTXuKYvueOPd2qjQVelW8doo2NapCPTWNyhT_NPHiv7BilLu3rdOxsvDamYYf17RmUzfWMXSgHW4YV7YlRgcBSmdrmORz3VfyHpPv_eRBdKLUljzDoQ3LU8B0Fqe4woSOtQwMyUiNxEK_PL6mxfy4Gz2jZ9dqbh1uC5Dme8tsCSzpyujPZTcSQ_rLKlY9dOm9EGG9IMEWH07OF2d2hjZVcYE2_PQaVdpndLG2tltTGtyqQhbGkI1JnLp3DGXv_waC3ymEsiXNRlb65w10g5cJJ-bdeJaB6d3skMWoHHcfVDGnqLldwn3yOPQKh2IrDTO7gX2UjSOp3sfDIevP8ZlElGY4N-Et_ALR2HogmgIAAA==

Logs

No response

System Info

System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M3 Pro
    Memory: 127.70 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.1 - ~/.nvm/versions/node/v20.12.1/bin/node
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.1/bin/npm
    pnpm: 9.1.2 - /opt/homebrew/bin/pnpm
  Browsers:
    Brave Browser: 125.1.66.118
    Safari: 17.5

Severity

annoyance

@brunnerh
Copy link
Member

brunnerh commented Jun 5, 2024

This is expected, you need derived to make the variables reactive:

let { count, increment } = $derived(createCounter());

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Jun 5, 2024
@timootten
Copy link

Hey, why is this expected?

It seems kind of strange to me that it doesn't work straight away:
let { count, increment } = createCounter();

@brunnerh
Copy link
Member

count is a primitive value, without any further added logic, the destructuring will cause it to be read (and thus copied) once and never change.

There is no good way for Svelte to know that it needs to add that extra logic here to keep updating the value.
Treating everything as potentially changing could cause other issues and adds overhead in computational cost and bundle sizes.

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

No branches or pull requests

4 participants