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

Svelte 5: derived store not reactively updated #10771

Closed
mquandalle opened this issue Mar 12, 2024 · 2 comments · Fixed by #10785
Closed

Svelte 5: derived store not reactively updated #10771

mquandalle opened this issue Mar 12, 2024 · 2 comments · Fixed by #10785
Assignees
Milestone

Comments

@mquandalle
Copy link

Describe the bug

In the following code snippet I expect the store countChecked to be recalculated whenever checks is modified:

import { derived, writable } from "svelte/store";

const checks = writable([false, false, false])
const countChecked = derived(checks, ($checks) => $checks.filter(Boolean).length)

It works on Svelte 4 but is broken on Svelte 5.

Reproduction

Svelte 5 REPLSvelte 4 REPL

Logs

No response

System Info

System:
    OS: macOS 14.3.1
    CPU: (8) arm64 Apple M2
    Memory: 128.69 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.0/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    pnpm: 8.14.1 - ~/.nvm/versions/node/v18.16.0/bin/pnpm
    bun: 1.0.25 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 114.1.52.126
    Safari: 17.3.1
  npmPackages:
    svelte: next => 5.0.0-next.75

Severity

blocking an upgrade

@dummdidumm dummdidumm self-assigned this Mar 12, 2024
@dummdidumm dummdidumm added this to the 5.0 milestone Mar 12, 2024
@rmunn
Copy link
Contributor

rmunn commented Mar 12, 2024

Note that in runes mode you get the error "Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. 'array[i] = value' instead of 'entry = value')", and if you rewrite the Svelte 5 repro to that style, it works. E.g. if the each block looks like this:

{#each $checks as checked, i}
	<input type="checkbox" bind:checked={$checks[i]} />
{/each}

Then the derived store is updated correctly, in both runes and non-runes modes.

@adminy
Copy link

adminy commented Mar 13, 2024

It sounds to be related to my issue: #10689

I'm trying to reactively update $state variable from a derived store builtin to sveltekit.

Rich-Harris pushed a commit that referenced this issue Mar 13, 2024
* fix: invalidate store when mutated inside each block

fixes #10771

* Update packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js
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

Successfully merging a pull request may close this issue.

4 participants