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: {#each expression as name (key)} behaves differently than Svelte 4 #11232

Closed
qupig opened this issue Apr 18, 2024 · 8 comments · Fixed by #11254
Closed

Svelte 5: {#each expression as name (key)} behaves differently than Svelte 4 #11232

qupig opened this issue Apr 18, 2024 · 8 comments · Fixed by #11254
Labels
Milestone

Comments

@qupig
Copy link

qupig commented Apr 18, 2024

Describe the bug

{#each expression as name (key)}...{/each}

The issue is that the behavior of key in the above syntax is different in Svelte 4 and 5.

It took a lot of time to lock in to this and make an intuitive minimal reproduction.

Strangely, somehow it couldn't be reproduced in the official Svelte5-REPL, so I had to use StackBlitz to provide an online preview.

You can see in the demo how the actual rendering of the component elements behaves differently when changing the order of the array elements.

Reproduction

StackBlitz-svelte-4.2.15

StackBlitz-svelte-5.0.0-next.108

Svelte5-REPL (Unable to reproduce)

svelte5.0.0-next.108.mp4

Logs

No response

System Info

svelte: 5.0.0-next.108

Severity

blocking an upgrade

@qupig qupig changed the title Svelte 5: {#each expression as name (key)} behaves differently than Svelte 4 Svelte 5: {#each expression as name (key)} behaves differently than Svelte 4 Apr 18, 2024
@Thiagolino8
Copy link

It doesn't happen in the production build, it probably has something to do with HMR

@qupig
Copy link
Author

qupig commented Apr 19, 2024

It doesn't happen in the production build, it probably has something to do with HMR

Yes, I can confirm this, which may explain why the issue is not reproduced in Svelte5-REPL.

@gterras
Copy link

gterras commented Apr 19, 2024

Can you really use the value itself as a key? I've always thought it was not at all expected but it may be a relic from my Vue days since it isn't mentioned in the docs.

Anyway if you {#each array.entries() as [i, item] (i)} it starts working as expected.

@Thiagolino8
Copy link

Can you really use the value itself as a key? I've always thought it was not at all expected but it may be a relic from my Vue days since it isn't mentioned in the docs.

Yes you can
This has always been one of Svelte's differentiators.

The key can be any object, but strings and numbers are recommended since they allow identity to persist when the objects themselves change.

@gterras
Copy link

gterras commented Apr 19, 2024

Yes you can

Yes although I have trouble seeing what's the under the hood difference in this case VS not using at all a key? Since the keys should also be modified along the values? Worth noting that both repros work as expected without key.

@qupig
Copy link
Author

qupig commented Apr 19, 2024

@gterras

Anyway if you {#each array.entries() as [i, item] (i)} it starts working as expected.

No, I tried that version and it produces the same effect.
Using the same value is just to simplify reproduction.

I can adapt this to a new reproduction of that syntax if you need it.

Here you go:
StackBlitz-svelte-5.0.0-next.108 (with k,v syntax)

@Rich-Harris Rich-Harris added this to the 5.0 milestone Apr 19, 2024
@Rich-Harris
Copy link
Member

I haven't figured out why, exactly, but this is related to HMR — if I disable it in the Vite config, things work as expected. That's why you can't reproduce it in the REPL

@Rich-Harris
Copy link
Member

I tell a lie. HMR is a factor here, insofar as it's what accounts for the discrepancy between repros, but it's actually any effect inside an {#each ...} loop, not just the HMR effect. Remarkable that it wasn't surfaced sooner.

It was introduced in #11197 (demo), and is fixed in #11254.

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

Successfully merging a pull request may close this issue.

4 participants