Keyed each blocks help Svelte track list items by a unique key. This makes updating lists smoother and faster.
Keyed each blocks in Svelte
{#each items as item (item.id)}
<!-- content -->
{/each}The part in parentheses (item.id) is the unique key for each item.
Keys should be unique and stable for each item to work well.
users and use user.id as the key.{#each users as user (user.id)}
<p>{user.name}</p>
{/each}task.taskId as the unique key for each task.{#each tasks as task (task.taskId)}
<li>{task.title}</li>
{/each}{#each numbers as number (number)}
<span>{number}</span>
{/each}This Svelte component shows a list of fruits. When you click the button, the list order changes randomly. Using a keyed each block with fruit.id helps Svelte update the list efficiently without losing track of each fruit.
<script> let fruits = [ { id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, { id: 3, name: 'Cherry' } ]; function shuffle() { fruits = fruits .map(value => ({ value, sort: Math.random() })) .sort((a, b) => a.sort - b.sort) .map(({ value }) => value); } </script> <button on:click={shuffle}>Shuffle Fruits</button> <ul> {#each fruits as fruit (fruit.id)} <li>{fruit.name}</li> {/each} </ul>
Always use a unique and stable key to avoid bugs.
If you don't use keys, Svelte will update list items by position, which can cause unexpected behavior.
Keys help with animations and preserving input states inside list items.
Keyed each blocks use a unique key to track list items.
This improves list updates and keeps UI consistent.
Use keys whenever your list can change dynamically.
Practice
keyed each block in Svelte?Solution
Step 1: Understand what keyed each blocks do
Keyed each blocks use a unique key to track each item in a list, helping Svelte update only changed items.Step 2: Identify the main purpose
This unique key helps keep the UI consistent and efficient when the list changes dynamically.Final Answer:
To uniquely identify list items for efficient updates -> Option BQuick Check:
Keyed each blocks track items uniquely = D [OK]
- Thinking keys are for styling
- Assuming keys sort items
- Believing keys stop rendering
Solution
Step 1: Recall keyed each block syntax
The correct syntax uses parentheses after the 'as' clause to specify the key: {#each items as item (item.id)}Step 2: Compare options
{#each items as item (item.id)}matches the correct syntax exactly, others miss parentheses or order.Final Answer:
{#each items as item (item.id)} -> Option AQuick Check:
Key goes in parentheses after 'as' = B [OK]
- Omitting parentheses around the key
- Placing key before 'as'
- Using wrong order of parameters
{#each users as user (user.id)}
{user.name}
{/each}What happens if the
users array changes by adding a new user with a unique id?Solution
Step 1: Understand keyed each block behavior on list change
When a new item with a unique key is added, Svelte updates only that item in the DOM.Step 2: Analyze the effect of adding a new user
The existing users remain unchanged; only the new user is inserted efficiently.Final Answer:
Only the new user is added to the DOM without re-rendering existing users -> Option AQuick Check:
Keyed blocks update only changed items = C [OK]
- Thinking all items re-render on change
- Believing list updates require page reload
- Assuming keys must be strings only
{#each items as item (item.key)}
{item.value}
{/each}If
item.key is not unique for all items, what problem can occur?Solution
Step 1: Understand the role of unique keys
Keys must be unique to correctly track each item; duplicates confuse Svelte's DOM updates.Step 2: Identify the effect of duplicate keys
With duplicate keys, Svelte may wrongly reuse DOM elements, causing wrong data to show or UI glitches.Final Answer:
Svelte may reuse DOM elements incorrectly, causing UI bugs -> Option DQuick Check:
Duplicate keys cause wrong DOM reuse = A [OK]
- Expecting syntax errors from duplicates
- Thinking list won't render at all
- Assuming Svelte fixes duplicates automatically
{#each tasks as task (???)}
{task.name}
{/each}Solution
Step 1: Identify the best key for dynamic lists
Keys must be unique and stable to track items correctly during reordering.Step 2: Compare key options
Task IDs are unique and stable, while names can duplicate and indexes change on reorder.Final Answer:
Use task.id because IDs are unique and stable keys -> Option CQuick Check:
Unique stable keys like IDs are best for dynamic lists = A [OK]
- Using non-unique names as keys
- Using index keys causing wrong updates
- Skipping keys on dynamic lists
Ready to prove it?
Pass the test and this pattern moves unknown
