Svelte · Junior · Question 19 of 76
Why shouldn't you use the index as the key in {#each}?
- ✓Correct answer: When items are added, removed or reordered, indexes shift and Svelte matches the wrong DOM nodes and state
- 2Index keys are not supported and cause a compile error
- 3Index keys make the list render twice
Explanation
Use a stable unique identifier like item.id. The index is available as the second variable: {#each items as item, i (item.id)}.