0
0
Svelteframework~5 mins

Keyed each blocks in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a keyed each block in Svelte?
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: <br> {#each items as item (item.id)} <br> 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.
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.
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.
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.
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.
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.