Skip to main content
In Svelte 5, the component lifecycle consists of two main parts: creation and destruction. Everything in between is handled by reactive effects that update specific parts of the UI when state changes.

Lifecycle Overview

A component’s lifecycle:
  1. Creation - Component is instantiated and mounted to the DOM
  2. Updates - Reactive effects run when dependencies change
  3. Destruction - Component is unmounted and cleanup occurs
1
Component Creation
2
When a component is created:
3
  • The <script> block executes
  • State is initialized
  • Props are received
  • Effects are scheduled
  • DOM is created and mounted
  • onMount callbacks run
  • 4
    Reactive Updates
    5
    During the component’s lifetime:
    6
  • $effect runs when dependencies change
  • $derived values recompute automatically
  • DOM updates are batched and applied
  • No “before update” or “after update” hooks
  • 7
    Component Destruction
    8
    When a component is destroyed:
    9
  • Effect cleanup functions run
  • onDestroy callbacks execute
  • DOM is removed
  • Memory is freed
  • onMount

    The onMount function schedules a callback to run when the component is mounted to the DOM:

    Key Characteristics

    • Runs once per component instance
    • Executes after the component is mounted to the DOM
    • Does not run during server-side rendering
    • Can be called from external modules during initialization

    Cleanup with onMount

    Return a function from onMount to run cleanup when the component unmounts:
    Note: This only works with synchronous functions. Async functions always return a Promise.

    Common Use Cases

    onDestroy

    Schedules a callback to run immediately before the component is unmounted:

    Server-Side Rendering

    onDestroy is the only lifecycle hook that runs during server-side rendering:

    Effects for Reactivity

    Use $effect for side effects that should run when dependencies change:

    Effect Timing

    Effects run:
    • After the component is mounted
    • In a microtask after state changes
    • Updates are batched for performance
    • After DOM updates are applied

    Effect Cleanup

    Return a cleanup function to run when the effect re-runs or the component is destroyed:

    Pre-DOM Update Effects

    Use $effect.pre to run code before the DOM updates:

    tick() Function

    Use tick to wait for pending DOM updates:

    Deprecated: beforeUpdate/afterUpdate

    These hooks from Svelte 4 are deprecated. Use $effect.pre and $effect instead:

    Advanced: $effect.root

    Create effects outside component initialization:

    Lifecycle Best Practices

    1. Use onMount for initialization - Fetch data, set up subscriptions, initialize libraries
    2. Use onDestroy for cleanup - Clear timers, unsubscribe, remove event listeners
    3. Prefer $effect for reactivity - Let Svelte track dependencies automatically
    4. Use $effect.pre sparingly - Only when you need pre-DOM-update timing
    5. Avoid state updates in effects - Use $derived instead when possible

    Effect Dependencies

    Effects automatically track dependencies:
    Dependencies are tracked synchronously - values read after await or in setTimeout are not tracked.