Svelte Key Blocks
A key block destroys and rebuilds its content whenever a chosen value changes. Svelte normally reuses existing elements and only patches their text. A key block overrides that habit and forces a fresh start.
Key Block Syntax
Wrap any markup or component in a key block and name the value that triggers the rebuild.
{#key currentPage}
<section>
<h3>Page {currentPage}</h3>
</section>
{/key}Each time currentPage changes, Svelte removes the old section and creates a new one. When the value stays the same, nothing happens.
Rebuild Diagram
currentPage = 1
|
v
[ Section for page 1 ] <-- on screen
currentPage changes to 2
|
v
Old section destroyed
|
v
New section created for page 2
|
v
[ Section for page 2 ] <-- on screenReplaying Transitions
Transitions play when an element enters or leaves the page. A changing text value alone does not trigger one. A key block turns every change into a leave and an enter, so the animation plays again.
<script>
import { fade } from "svelte/transition";
let tips = ["Drink water", "Stretch daily", "Sleep early"];
let index = 0;
</script>
<button on:click={() => (index = (index + 1) % tips.length)}>
Next Tip
</button>
{#key index}
<p in:fade>{tips[index]}</p>
{/key}Every button press fades in a new tip. Without the key block, the text would change abruptly.
Resetting Component State
A component keeps its internal variables while it stays on the page. Switching the user in a profile form can leave old typed text behind. A key block tied to the user id builds a clean form each time.
<script>
import EditForm from "./EditForm.svelte";
export let userId;
</script>
{#key userId}
<EditForm {userId} />
{/key}The EditForm component restarts with empty fields whenever userId changes. All its local variables, timers, and lifecycle functions start over.
Key Blocks Versus Keyed Each Blocks
The two features share a name and solve different problems. A keyed each block tracks list items by identity so Svelte can move, add, or remove the right rows. A key block rebuilds one chunk of markup when a value changes.
Feature Syntax Goal
---------------- ----------------------------- ------------------------
Keyed each {#each rows as row (row.id)} Track list items
Key block {#key value}...{/key} Rebuild on value changeA Layman Analogy
Imagine a whiteboard in a classroom. Normally the teacher erases a few words and writes new ones, leaving the rest of the board alone. A key block works like replacing the entire whiteboard with a brand new, clean one. The old marks vanish completely, and the class sees a fresh start.
When To Use A Key Block
- Replaying an in or out transition for changing content
- Resetting a child component when its identity changes
- Restarting a timer or animation tied to a value
Common Mistakes To Avoid
- Wrapping large sections of the page in a key block, which rebuilds too much work
- Keying on an object that changes identity on every update
- Reaching for a key block when a reactive variable would update the page smoothly
Key Points
- A key block destroys and recreates its content when the key value changes
- It replays transitions and resets child component state
- It differs from a keyed each block, which tracks list items
- Small, focused key blocks keep pages fast
