0Pricing
Sveltejs Academy · Lesson

{#key} for Forcing Re-render

Use the key block to destroy and recreate components when a value changes.

The key Block

{#key expr} destroys and recreates its contents whenever expr changes. Useful when you want a fresh component.

Restart Animations

Wrap an animated element in {#key} to replay the transition each time the key changes.

{#key id}
  <div in:fade>{message}</div>
{/key}

All lessons in this course

  1. {#if} {#else if} {#else}
  2. {#each} with Index and Key
  3. {#key} for Forcing Re-render
  4. Nested {#each} and Keyed Updates
← Back to Sveltejs Academy