mirror of https://github.com/sveltejs/svelte
fix: detect and error on non-idempotent each block keys in dev mode (#17732)
## Summary Fixes #17721 In dev mode, detect when a keyed each block has a key function that returns different values when called multiple times for the same item (non-idempotent). This catches the common mistake of using array literals like `[thing.group, thing.id]` as keys, which creates a new array object each time and will never match by reference. - Adds new `each_key_volatile` error with helpful message explaining the issue - Checks key idempotency in the each block loop during dev mode - Provides a clear error instead of the cryptic "Cannot read properties of undefined" that occurred previously --------- Co-authored-by: 7nik <kifiranet@gmail.com>pull/17733/head
parent
3f6521df0e
commit
2287ad005a
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: detect and error on non-idempotent each block keys in dev mode
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
compileOptions: {
|
||||||
|
dev: true
|
||||||
|
},
|
||||||
|
|
||||||
|
mode: ['client'],
|
||||||
|
|
||||||
|
error: 'each_key_volatile'
|
||||||
|
});
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<script>
|
||||||
|
let things = $state([
|
||||||
|
{ group: 'a', id: 1 },
|
||||||
|
{ group: 'b', id: 2 }
|
||||||
|
]);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#each things as thing ([thing.group, thing.id])}
|
||||||
|
<p>{thing.group}-{thing.id}</p>
|
||||||
|
{/each}
|
||||||
Loading…
Reference in new issue