A keyed each block in Svelte is a way to loop over a list where each item is tracked by a unique key. This helps Svelte update only the changed items in the list, improving performance and keeping the UI in sync.
Click to reveal answer
beginner
Why should you use a key in an each block?
Using a key helps Svelte identify which items changed, moved, or were removed. This prevents unnecessary re-rendering and keeps animations and transitions smooth.
Click to reveal answer
beginner
How do you write a keyed each block in Svelte?
You write it like this: {#each items as item (item.id)} The part in parentheses is the key, usually a unique id from the item.
Click to reveal answer
intermediate
What happens if you don’t use keys in an each block?
Svelte will update the list by position, which can cause wrong items to update or lose their state. This can lead to bugs or weird UI behavior.
Click to reveal answer
intermediate
Can you use any value as a key in a keyed each block?
The key should be a unique and stable value for each item, like an id. Using indexes or non-unique values can cause problems when the list changes.
Click to reveal answer
What is the main benefit of using a keyed each block in Svelte?
AIt disables animations.
BIt makes the list items bigger.
CIt helps Svelte track items uniquely for efficient updates.
DIt sorts the list automatically.
✗ Incorrect
Keyed each blocks let Svelte know which items changed by using unique keys, so it updates only those items.
How do you specify the key in a Svelte each block?
AInside parentheses after the item, like (item.id).
BBy adding a key attribute to the HTML element.
CBy using a special key function.
DBy writing #key before the each block.
✗ Incorrect
The key is placed in parentheses after the item in the each block syntax.
What can happen if you use the array index as a key?
AIt can cause incorrect updates when the list changes.
BIt improves performance.
CIt prevents the list from rendering.
DIt automatically sorts the list.
✗ Incorrect
Using indexes as keys can confuse Svelte when items move or change, causing wrong updates.
Which of these is a good choice for a key in a keyed each block?
AA random number generated each render.
BThe item's position in the list.
CThe item's name if it can repeat.
DA unique id from the item.
✗ Incorrect
A unique and stable id ensures Svelte can track each item correctly.
If you omit the key in an each block, what does Svelte do?
AThrows an error and stops rendering.
BUpdates items by their position in the list.
CAutomatically generates unique keys.
DSkips rendering the list.
✗ Incorrect
Without keys, Svelte updates list items by their order, which can cause UI issues.
Explain what a keyed each block is in Svelte and why it is useful.
Think about how Svelte tracks list items when they change.
You got /3 concepts.
Describe how to write a keyed each block in Svelte with an example.
Remember the parentheses after the item in the each block.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a keyed each block in Svelte?
easy
A. To prevent the list from rendering
B. To uniquely identify list items for efficient updates
C. To sort the list items automatically
D. To style list items differently
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 B
Quick Check:
Keyed each blocks track items uniquely = D [OK]
Hint: Keys track items uniquely for smooth updates [OK]
Common Mistakes:
Thinking keys are for styling
Assuming keys sort items
Believing keys stop rendering
2. Which of the following is the correct syntax for a keyed each block in Svelte?
easy
A. {#each items as item (item.id)}
B. {#each items as item}
C. {#each items (item.id)}
D. {#each items item.id as item}
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 A
Quick Check:
Key goes in parentheses after 'as' = B [OK]
Hint: Key is inside parentheses after 'as' in each block [OK]
Common Mistakes:
Omitting parentheses around the key
Placing key before 'as'
Using wrong order of parameters
3. Given the code:
{#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?
medium
A. Only the new user is added to the DOM without re-rendering existing users
B. All users are re-rendered from scratch
C. The list will not update until the page reloads
D. An error occurs because keys must be strings
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 A
Quick Check:
Keyed blocks update only changed items = C [OK]
Hint: New unique keys add items without full re-render [OK]
Common Mistakes:
Thinking all items re-render on change
Believing list updates require page reload
Assuming keys must be strings only
4. Consider this Svelte code snippet:
{#each items as item (item.key)}
{item.value}
{/each}
If item.key is not unique for all items, what problem can occur?
medium
A. Svelte will automatically fix duplicate keys
B. The code will throw a syntax error
C. The list will not render at all
D. Svelte may reuse DOM elements incorrectly, causing UI bugs
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 D
Quick Check:
Duplicate keys cause wrong DOM reuse = A [OK]
Hint: Keys must be unique to avoid UI bugs [OK]
Common Mistakes:
Expecting syntax errors from duplicates
Thinking list won't render at all
Assuming Svelte fixes duplicates automatically
5. You have a list of tasks with possible duplicate names but unique IDs. You want to display them with a keyed each block and allow dynamic reordering. Which key should you use and why?
{#each tasks as task (???)}
{task.name}
{/each}
hard
A. Use the index like each tasks as task (index) for simplicity
B. Use task.name because names are easier to read
C. Use task.id because IDs are unique and stable keys
D. No key is needed for dynamic lists
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 C
Quick Check:
Unique stable keys like IDs are best for dynamic lists = A [OK]
Hint: Use unique stable IDs as keys for dynamic lists [OK]
Common Mistakes:
Using non-unique names as keys
Using index keys causing wrong updates
Skipping keys on dynamic lists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.