Skip to content

Components & Composition

Props pass values down. But real UIs need to pass markup down (a card that wraps arbitrary content), share data across a subtree without drilling props through every level, and run setup when a component mounts. This module covers those composition tools:

  • Snippets ({#snippet} / {@render}) — reusable chunks of markup, and Svelte 5’s replacement for the old <slot>.
  • Passing UI — how a parent hands markup to a child via the children snippet and named snippet props.
  • Context (setContext / getContext) — sharing data down a component tree without prop drilling.
  • Lifecycle (onMount / onDestroy / tick) — running code at specific moments.
LessonWhat you’ll learn
Snippets & render{#snippet} and {@render}, replacing <slot>
Passing UI & childrenThe children snippet and named snippet props
The context APIsetContext / getContext without prop drilling
LifecycleonMount, onDestroy, tick, and $effect
flowchart TB
  comp["composing components"] --> snip["snippets: reusable markup"]
  comp --> children["passing UI down as children"]
  comp --> ctx["context: data down a subtree"]
  comp --> life["lifecycle: code at mount / destroy"]
The composition tools
What replaced `<slot>` in Svelte 5?
What problem does context (`setContext`/`getContext`) solve?
In Svelte 5, what covers most cases that used lifecycle hooks in Svelte 4?