Components & Composition
Composing beyond props
Section titled “Composing beyond props”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
childrensnippet 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.
What this module covers
Section titled “What this module covers”| Lesson | What you’ll learn |
|---|---|
| Snippets & render | {#snippet} and {@render}, replacing <slot> |
| Passing UI & children | The children snippet and named snippet props |
| The context API | setContext / getContext without prop drilling |
| Lifecycle | onMount, 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"]