0
0
Svelteframework~5 mins

Each blocks ({#each}) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the {#each} block in Svelte?
The {#each} block is used to loop over an array or list and render a block of HTML for each item in that list.
Click to reveal answer
beginner
How do you access the current item inside an {#each} block?
You give a name to the current item in the syntax: {#each items as item}, then use that name inside the block to access the item.
Click to reveal answer
intermediate
Why should you provide a unique key when using {#each} blocks?
Providing a unique key helps Svelte track each item efficiently when the list changes, improving performance and avoiding rendering bugs.
Click to reveal answer
beginner
Write the syntax to loop over an array called 'fruits' and display each fruit name inside a <li> element.
    {#each fruits as fruit}
  • {fruit}
  • {/each}
Click to reveal answer
beginner
What happens if the array used in {#each} is empty?
Nothing is rendered inside the {#each} block unless you use an {:else} block to show alternative content.
Click to reveal answer
What does the {#each} block do in Svelte?
ALoops over a list and renders HTML for each item
BDefines a function
CImports a component
DDeclares a variable
How do you write an {#each} block for an array named 'tasks' with item named 'task'?
A{#each task}
B{#each task as tasks}
C{#each tasks as task}
D{#each tasks}
Why is it recommended to add a key in {#each} blocks?
ATo style the list
BTo declare variables
CTo import data
DTo improve rendering performance and track items
What will happen if the array in {#each} is empty and no {:else} block is provided?
ANothing will be rendered inside the block
BAn error will occur
CThe first item will be repeated
DThe whole page will reload
Which of the following is the correct way to add a key to an {#each} block?
A{#each items as item key=item.id}
B{#each items as item (item.id)}
C{#each items with key item.id}
D{#each items item.id}
Explain how to use the {#each} block in Svelte to display a list of names.
Think about how you tell Svelte to repeat HTML for each name.
You got /3 concepts.
    Describe why adding a unique key in an {#each} block is important and how to do it.
    Keys help Svelte know which items changed when the list updates.
    You got /3 concepts.