Using Keyed Each Blocks in Svelte
📖 Scenario: You are building a simple Svelte app that shows a list of tasks. Each task has a unique ID and a name. You want to display these tasks in a way that Svelte can track each item efficiently when the list changes.
🎯 Goal: Create a Svelte component that uses a keyed {#each} block to render a list of tasks. Each task should have a unique id used as the key.
📋 What You'll Learn
Create an array of task objects with exact IDs and names
Add a variable to hold the number of tasks
Use a keyed
{#each} block with task.id as the keyRender each task's name inside a
<li> element💡 Why This Matters
🌍 Real World
Keyed each blocks help Svelte track list items efficiently when items are added, removed, or reordered, improving performance and user experience in dynamic lists.
💼 Career
Understanding keyed each blocks is essential for building interactive user interfaces in Svelte, a popular modern frontend framework used in web development jobs.
Progress0 / 4 steps