Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →In Svelte 5, declare a reusable template fragment with {#snippet name(parameters)}...{/snippet}, then insert it with {@render name(arguments)}. Snippets are useful for repeated markup inside a component and for letting a caller provide markup to another component. They remain scoped template constructs—not a universal replacement for components.
How Svelte snippets work
A snippet gives a name to a piece of markup so you can render it in more than one place. Parameters let each rendering use different data. Svelte’s official guide demonstrates this with a figure template used in conditional branches: Svelte snippet reference.
As an Amazon Associate I earn from qualifying purchases.
{#snippet figure(image)}
<figure>
<img src={image.src} alt={image.caption} />
<figcaption>{image.caption}</figcaption>
</figure>
{/snippet}
{#each images as image}
{#if image.href}
<a href={image.href}>{@render figure(image)}</a>
{:else}
{@render figure(image)}
{/if}
{/each}
Here, the snippet owns the shared figure markup, while the surrounding loop and condition decide which images are linked. The image parameter makes the data needed by each rendering explicit.
Where snippets fit—and where they do not
Use a snippet when a small markup fragment repeats locally, or when a component should let its caller customize how part of its content is rendered. A separate component is often clearer when the UI has its own responsibilities, state, lifecycle, or broad reuse needs. That is a design choice, not a Svelte compiler rule.
#1 Best Overall
| Question | Snippet | Separate component |
|---|---|---|
| Where does reuse happen? | Within a component, or as markup supplied to another component. | Across component boundaries as an independently defined unit. |
| Who controls the markup? | The component defining the snippet, or its caller when the snippet is passed in. | The component that owns the markup. |
| How does item-specific data get in? | Pass it as a snippet parameter, such as {@render row(item)}. |
Pass data through the component’s props. |
| What content-passing syntax applies? | Svelte 5 snippets and {@render ...}. |
A component boundary; its internal design determines how it accepts content. |
How to pass a snippet to another component
A component can receive a snippet as a named prop, or a caller can declare a snippet inside the component element so it is supplied implicitly. The child decides where to render it and what arguments to pass.
<Table data={fruits} {header} {row} />
In this pattern, Table can render a no-argument header with {@render header()} and a row for each data item with {@render row(item)}. The official guide uses this approach to let the caller control table markup: Svelte snippet reference.
Rank #2
Implicit children content
Content written between a component’s opening and closing tags becomes its implicit children snippet. The child reads it from $props() and renders it with {@render children()}. For example, ordinary content such as <Button>click me</Button> is received as children. If the component also needs content between its tags, do not also pass a separate children prop.
Free tools Windows power users keep installed
One-click scans. No signup required.
Optional snippet props
If a snippet prop may be absent, render it conditionally or use optional chaining. For example, {@render header?.()} avoids rendering an unavailable header. An {#if} branch is useful when the absent case needs fallback markup.
Rank #3
Typing snippet props with TypeScript
Import Snippet as a type from svelte. Use Snippet for a snippet with no parameters, and Snippet<[T]> for one that accepts a value of type T. A generic table can keep its data and row renderer aligned:
<script lang="ts" generics="T">
import type { Snippet } from 'svelte';
let {
data,
row
}: {
data: T[];
row: Snippet<[T]>;
} = $props();
</script>
With this type relationship, the row snippet’s argument corresponds to each element in data. See the official snippet reference for the documented typing patterns.
Rank #4
Scope, nesting, and exporting
Snippets follow lexical scope: a snippet can refer to values visible where it is declared, and it can be nested, recursive, or mutually referential. A snippet declared inside a block is not necessarily available outside that block, so declare it where every intended render site can access it.
Exporting is narrower than ordinary in-component reuse. The Svelte documentation says a top-level snippet can be exported from a <script module> only when it does not directly or indirectly reference declarations from a non-module script. The documented module-script export feature requires Svelte 5.5.0 or newer: Svelte snippet reference.
Snippets versus slots in Svelte 5
Svelte 5 deprecates slots in favor of snippets for passing markup. For new Svelte 5 code, use snippets and {@render ...}. Do not combine legacy <slot> syntax and render tags in the same component; the compiler reports an error for that combination. Existing Svelte 4 projects may still use older APIs, so account for the project’s version and migration state. See the compiler errors reference.
Quick Recap
Common snippet mistakes
- Using a snippet like an ordinary value: a snippet is rendered with
{@render snippet()}, not by writing{snippet}. - Rendering an optional snippet unconditionally: use optional chaining or an
{#if}branch with a fallback. - Mixing legacy slot patterns with implicit children: combining
let:directives with the implicitchildrensnippet render pattern is invalid; use a named snippet in that case. See Svelte runtime errors. - Declaring a snippet outside its usable scope: check whether the declaration sits inside a block that limits where it can be referenced.
How to choose
- Choose a snippet for repeated, localized markup or caller-supplied rendering customization.
- Pass item data as snippet parameters when the same template needs to render different rows, images, or other values.
- Choose a separate component when the UI is an independently meaningful unit with its own responsibilities or state.
- Use Svelte 5 snippet syntax for new markup-passing code, while treating existing slot-based code according to its current version and migration needs.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




