Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 35 ยท Components

Snippets and Children

Pass markup into components with Svelte 5 snippets: the children prop, named snippets with parameters, {@render}, and why they replace slots.

Sometimes a component should not just receive data but also markup: a Card that wraps whatever you place inside it, a Table whose rows you design yourself, a Modal with a custom footer. In Svelte 5 this is done with snippets, which replace the slots of earlier versions.

The children prop

Anything you write between a component's tags becomes a snippet called children, passed as a prop. The component draws it with {@render children()}:

SVELTE
<!-- Card.svelte -->
<script>
  let { title, children } = $props();
</script>

<section class="card">
  <h2>{title}</h2>
  {@render children?.()}
</section>
SVELTE
<!-- usage -->
<Card title="Welcome">
  <p>Thanks for visiting.</p>
  <button>Start</button>
</Card>

The optional call children?.() renders nothing if the parent passed no content. Without the ?., a missing children would throw.

Named snippets

Declare snippets with {#snippet name(params)} inside the component tag. Each one is passed as a prop of the same name:

SVELTE
<!-- Dialog.svelte -->
<script>
  let { header, children, footer } = $props();
</script>

<div class="dialog">
  <header>{@render header()}</header>
  <main>{@render children()}</main>
  {#if footer}
    <footer>{@render footer()}</footer>
  {/if}
</div>
SVELTE
<Dialog>
  {#snippet header()}
    <h2>Delete file?</h2>
  {/snippet}

  <p>This cannot be undone.</p>

  {#snippet footer()}
    <button>Cancel</button>
    <button>Delete</button>
  {/snippet}
</Dialog>

Content not inside a {#snippet} becomes children.

Snippets at a glance

Define {#snippet row(item)}
Pass As a prop or children
Render {@render row(x)}

A snippet is a reusable chunk of markup with parameters.

1 / 2

Snippets with parameters

Snippets can take arguments. This is how a generic component lets the parent decide how each item looks:

SVELTE
<!-- List.svelte -->
<script>
  let { items, row, empty } = $props();
</script>

{#if items.length}
  <ul>
    {#each items as item, i (item.id)}
      <li>{@render row(item, i)}</li>
    {/each}
  </ul>
{:else}
  {@render empty?.()}
{/if}
SVELTE
<List items={users}>
  {#snippet row(user, i)}
    <strong>{i + 1}.</strong> {user.name}
  {/snippet}
  {#snippet empty()}
    <p>No users yet.</p>
  {/snippet}
</List>

The component owns the data and the loop; the caller owns the look. Parameters can be destructured: {#snippet row({ name, age })}.

Snippets without components

Snippets are also useful inside one file to avoid repeating markup:

SVELTE
{#snippet badge(text, color)}
  <span class="badge" style:background={color}>{text}</span>
{/snippet}

{@render badge('New', 'seagreen')}
{@render badge('Sale', 'crimson')}

A snippet is visible to markup in the same scope and below. You can also export a snippet from a <script module> to share it between components, and you can pass a snippet around like any other value.

Why not slots?

Svelte 3 and 4 used <slot /> and <slot name="header" />, with let: for passing data back. They still work for compatibility, but snippets are clearer for three reasons:

  • They are ordinary values, so you can pass them through several components or store them in an object.
  • Parameters are explicit function-like arguments, not let: magic.
  • They work with TypeScript: you can type a snippet prop as Snippet<[Item]> imported from svelte.
SVELTE
<script lang="ts">
  import type { Snippet } from 'svelte';

  interface Props {
    items: Item[];
    row: Snippet<[Item, number]>;
    children?: Snippet;
  }
  let { items, row, children }: Props = $props();
</script>

Stand-in: snippets as functions

At heart a snippet is a function that returns markup, and {@render} calls it. This stand-in builds a generic list component that accepts row and empty render functions.

JS Render callbacks (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A reusable Card wrapper (stand-in page)

Common mistakes

  • Calling {@render children()} when no children were passed; use children?.() or check with {#if children}.
  • Naming a snippet and a prop the same by accident in a way you did not intend: snippets declared directly inside a component tag become props.
  • Mixing <slot> and {@render} in the same component; pick snippets in new code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the implicit prop name for content placed between a component's tags?

    Content outside any {#snippet} becomes the children snippet.
  2. 2. How do you draw a snippet in markup?

    {@render} calls a snippet with optional arguments.
  3. 3. Why write children?.() instead of children() in a wrapper component?

    Optional chaining skips the call when children is undefined.
  4. 4. Which statement best describes a snippet?

    Snippets are function-like markup you can pass around and render.

Summary

  • Content between component tags arrives as the children snippet; render it with {@render children?.()}.
  • Named snippets become props with the same name; they can take parameters.
  • Snippets also remove repetition inside a single file.
  • They replace slots and are typed with Snippet<[...]> in TypeScript.

Next: Callback Props and $bindable