0Pricing
Sveltejs Academy · Lesson

Passing Snippets as Props

Accept snippet values as props to build highly flexible component APIs.

Passing Snippets as Props is a free Sveltejs Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Snippet as a Prop

Snippets can be passed to child components like any other value.

Define and Pass

Define inline in the parent and reference by name.

{#snippet item(thing)}<li>{thing.name}</li>{/snippet}
<List itemSnippet={item} items={data} />

Receive in Child

Declare the snippet as a typed prop.

let { itemSnippet, items } = $props();
{#each items as thing}
  {@render itemSnippet(thing)}
{/each}

Render Props Pattern

This generalizes the React-style render prop. Parents control how children render.

Customization

Different parents can supply different snippets, fully customizing the layout.

Typed Snippet Props

With TypeScript, type the snippet param so child components get autocomplete.

let { itemSnippet }: { itemSnippet: Snippet<[Thing]> } = $props();

Optional Snippets

Snippet props can be optional; render only when provided.

{#if itemSnippet}
  {@render itemSnippet(thing)}
{/if}

Children Snippet

The default snippet is exposed as children in $props().

let { children } = $props();
{@render children()}

Composition Power

Snippet props are the foundation for headless and composable UI libraries.

TypeScript Import

Import the Snippet type from svelte for proper typing.

import type { Snippet } from "svelte";

Compared to Slots

Snippet props are more flexible than slots: they accept parameters and can be conditionally rendered.

Quick Check

How do you receive a snippet as a prop?

Recap

Pass snippets as props to invert control: parents supply rendering logic, children invoke it via @render.

Frequently asked questions

Is the “Passing Snippets as Props” lesson free?

Yes — the full text of “Passing Snippets as Props” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.

What will I learn in “Passing Snippets as Props”?

Accept snippet values as props to build highly flexible component APIs. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Passing Snippets as Props” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. $props() for Typed Props
  2. {#snippet} and {@render}
  3. Passing Snippets as Props
  4. Default and Named Snippets
← Back to Sveltejs Academy