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

$derived vs function in each block #14991

Closed
hermit99 opened this issue Jan 13, 2025 · 4 comments
Closed

$derived vs function in each block #14991

hermit99 opened this issue Jan 13, 2025 · 4 comments

Comments

@hermit99
Copy link

Describe the bug

I'm not sure if this is a bug or just something to discuss.

Essentially if I use a function directly in the each block, the function splitItem() is triggered twice whenever the item is mutated:

{#each splitItem(item) as it}
  <li>{it}</li>
{/each}

However it's as expected if I use $derived - splitItem() is called once when the item is changed. So I'm wondering why prop change triggered the callback twice in A.svelte?

Reproduction

Playground

Logs

No response

System Info

System:
    OS: Windows 11 10.0.26100
  Binaries:
    Node: 22.11.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.15.3 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (131.0.2903.86)
    Internet Explorer: 11.0.26100.1882

Severity

annoyance

dummdidumm added a commit that referenced this issue Jan 13, 2025
closes #14991 - at least to the extent possible

Also adjusts a comment which a) did not hint at why it's needed in the first place (was added in #14967) b) sounded like we could change that in the future, but we can't, because people will always have the ability to trigger reactivity through other means without changing the array reference
@trueadm
Copy link
Contributor

trueadm commented Jan 14, 2025

Has this since been fixed now?

@dummdidumm
Copy link
Member

No, there's still a double invocation, the question is whether or not we can safely remove the second get call at the end of the each function, which would resolve this

@trueadm
Copy link
Contributor

trueadm commented Jan 14, 2025

@dummdidumm Looks like we can't due to each blocks being special in that they mutate state internally which can cause the graph to become unstable.

@hermit99
Copy link
Author

Has this since been fixed now?

Yeah I reckon it’s fixed with v5.17.4

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.

3 participants