In Svelte 5, children is a function

By Esteban Trillo6 min readWhy SvelteSvelte

The markup gets mounted once, and the number on it keeps up because it's wired to a getter.
The markup gets mounted once, and the number on it keeps up because it's wired to a getter.

Part 7 of a series on Svelte in 2026. Part 6 covered props. This part covers the props that carry markup instead of data.

React and Svelte disagree about what children is. I built the same list component in React 19.3.1 and Svelte 5.57.2, with a heading nested inside the tags and a prop for rendering each row, and had both log the type of what they got:

React: typeof children: object | typeof row: function
Svelte: typeof children: function | typeof row: function

In React, the heading you nest inside a component arrives as an object, a description of elements that already exist. In Svelte 5 it arrives as a function. Svelte 4 had slots for this, a separate system with its own syntax. Svelte 5 deprecated them and made markup one more kind of value you pass around. The docs call those values snippets.1

Declaring and rendering

A snippet is a piece of template with a name and parameters. You declare it with {#snippet} and call it with {@render}. The docs’ example removes a duplicated <figure>:

{#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}

Parameters work like a function’s: defaults and destructuring are fine. Rest parameters aren’t, and the compiler says so: snippet_invalid_rest_parameter, “use an array instead.” A snippet can read anything in scope, like the component’s state or the current {#each} item, and it can call itself, which is how you render a tree.

What it compiles to

Here’s a counter that renders its value through a snippet. Probe is a component whose script logs one line, so I can count how often the snippet’s body runs:

App.svelte
<script>
import Probe from "./Probe.svelte";
let count = $state(0);
</script>
{#snippet badge(n)}
<Probe />
<span>count is {n}</span>
{/snippet}
<button onclick={() => count++}>+1</button>
{@render badge(count)}

Compiled for production (trimmed):

App.js (compiled, excerpt)
const badge = ($$anchor, n = $.noop) => {
Probe(node, {});
$.template_effect(() => $.set_text(text, `count is ${n() ?? ""}`));
};
export default function App($$anchor) {
let count = $.state(0);
badge(node_1, () => $.get(count));
}

Two things in there are the whole post.

The snippet really is a function. It takes the spot in the DOM to render into, then its parameters. Since badge doesn’t touch the component’s own variables, the compiler moved it out of the component entirely.

The argument isn’t count. It’s () => $.get(count), the same getter part 6 found in the props object. Inside the snippet, every read of n is a call to n(). I clicked three times and the text went 0, 1, 2, 3, while Probe logged once. The snippet body ran at mount. After that, only the text node changed.

Because a parameter is a getter, you can’t assign to it. n++ inside a snippet fails to compile with snippet_parameter_assignment.

Passing markup to a component

Snippets are values, so you pass them as props. Declare one inside a component’s tags and it becomes a prop named after it. Anything else inside the tags becomes the children snippet:1

App.svelte
<List {items}>
<strong>My list</strong>
{#snippet item(text)}
<span>{text.toUpperCase()}</span>
{/snippet}
</List>
List.svelte
<script>
let { items, item, empty, children } = $props();
</script>
{@render children?.()}
{#if items.length}
<ul>
{#each items as entry}
<li>{@render item(entry)}</li>
{/each}
</ul>
{:else}
{@render empty?.()}
{/if}

The compiled parent shows there’s no special channel. List gets item as a plain prop and children as an inline arrow function, right next to the items getter. Pushing 'three' onto items added a third row, rendered by the same snippet.

?.() renders the snippet only if the parent passed one. A parent that doesn’t pass empty leaves it undefined, like any missing prop.

With TypeScript, the snippet’s parameters become part of the component’s type. Snippet takes a tuple, one entry per parameter, and a generic ties the rows to the data:

List.svelte
<script lang="ts" generics="T">
import type { Snippet } from "svelte";
interface Props {
items: T[];
row: Snippet<[T]>;
empty?: Snippet;
}
let { items, row, empty }: Props = $props();
</script>

I passed items={[{ name: 'Ada', age: 36 }]} and read user.email inside the row. svelte-check 4.7.7 flagged it: Property 'email' does not exist on type '{ name: string; age: number; }'. Leaving out row was an error too.

A snippet that doesn’t use the component’s state can also be exported from <script module> and imported elsewhere, since 5.5.0.2

From slots to snippets

Slots still work in Svelte 5, and a parent can fill a slot with a snippet. The reverse doesn’t work: a component that uses {@render} can’t take slotted content.3 And one file can’t mix them: <slot> next to {@render} is a compile error, slot_snippet_conflict. So the migration goes child first, one component at a time. Pick what you used:

In Svelte 4 I used…
Before
<slot />
Svelte 5
{@render children?.()}
Notes
children is an ordinary prop. Destructure it from $props() like any other.
Before
<slot>Nothing here</slot>
Svelte 5
{#if children}{@render children()}{:else}Nothing here{/if}
Notes
The fallback is an {:else} branch, so it can be anything an if block holds.
Before
<slot name="header" /> and slot="header"
Svelte 5
{@render header?.()} and {#snippet header()}…{/snippet}
Notes
A snippet declared inside the component's tags becomes a prop with its name.
Before
<slot item={entry} /> and let:item
Svelte 5
{@render item(entry)} and {#snippet item(text)}…{/snippet}
Notes
The data goes in as an argument. sv migrate writes children({ item }) instead; rename it by hand if you want.
Before
{#if $$slots.footer}
Svelte 5
{#if footer}
Notes
A missing snippet is undefined, like any prop the parent didn't pass.
Before
<svelte:fragment slot="header">
Svelte 5
{#snippet header()}…{/snippet}
Notes
A snippet has no wrapper element, so there's nothing to work around.
Before
<slot name="header" slot="header" />
Svelte 5
<Inner {header} />
Notes
A snippet is a value. Pass it on like any other prop.

sv migrate svelte-5 does this conversion for you. I ran Svelte’s migrate() on a list that passed each entry with <slot item={entry} />. It produced {@render children?.({ item: entry, })} in the child and {#snippet children({ item })} in the parent. That compiled and rendered one and two. It’s a literal translation, though, and the widget’s let: row shows the version you’d write by hand.

List renders {@render row(entry)} for 2 items. The parent's row snippet shows {text} {count}, and count changes 3 times. How many times does the snippet's body run?

How React does it

React has two answers to the same job. Nested JSX arrives as children, a value: “the parent component will receive that content in a prop called children.”4 When the child needs to give the markup some data, you pass a function instead, a render prop. React’s docs say “there is nothing special about it: it is a regular prop which happens to be a function.”5

That’s close to a snippet, and React got there with no new syntax, which I like. The difference is when the function runs. I ported the list to React with a row render prop and clicked three times. React called row 8 times: two rows, four renders. In Svelte the row snippet’s body ran twice, once per row, and the clicks only rewrote two text nodes. That’s part 2 again: React calls your code again and diffs, Svelte runs it once and updates what changed.

React’s children being a value has one upside: you can inspect it. Children.map and Children.count exist, though React’s docs warn that “using Children is uncommon and can lead to fragile code.”5 Svelte doesn’t offer that at all. A snippet is opaque. You can render it, pass it on, or check whether it exists.

Gotchas

{@render children()} with no children. In dev, Svelte throws invalid_snippet: “Could not {@render} snippet due to the expression being null or undefined.” The production build rendered nothing and said nothing. Type children as optional and svelte-check makes you write ?.().

Slotted content into a snippet component disappears. A parent wrote <p slot="header"> for a child that renders {@render header?.()}. No error, no warning, and an empty header.

A children prop beats the content. I passed children={s} and put text inside the tags. The child rendered the prop, and the text was gone. The docs say not to name a prop children; the compiler doesn’t stop you.1

Snippets are scoped like blocks. A snippet declared inside another one isn’t visible outside it. Svelte compiled {@render y()} from outside its scope without complaint, then threw ReferenceError: y is not defined at mount. svelte-check catches it: “Cannot find name ‘y’.”

A snippet isn’t a function you call from script. Top-level snippets are visible in <script>, so you can pick one there and pass it along. Calling greet('x') directly threw invalid_snippet_arguments in dev, and in production anchor.before is not a function. Render it with {@render}.

Exported snippets can’t reach instance state. export { greet } from <script module>, with greet reading a $state from the instance script, fails with snippet_invalid_export.

Markup is a value now

Svelte 4 had props for data and slots for markup, with let: to send data back up into the slot. Svelte 5 has props, and some of those props are functions that render markup. Their arguments are getters, so the body runs once and stays live. Most of the rules above follow from that, including the type errors you now get for free.

Read the snippet page first; it’s short and covers scope, typing and exports.1 The “Snippets instead of slots” section of the migration guide has the slot-by-slot diffs and the maintainers’ reasons for the change.3

Next: shared state in .svelte.ts modules.

  • Part 8 — Shared state in .svelte.ts modules: global stores without stores: $state in a module any component can import, what you can export, and why the server needs context

Footnotes

  1. Svelte docs, {#snippet ...} and {@render ...}. ↩ ↩2 ↩3 ↩4

  2. Svelte changelog, 5.5.0: “allow snippets to be exported from module scripts” (#14315). ↩

  3. Svelte docs, Svelte 5 migration guide. ↩ ↩2

  4. React docs, Passing Props to a Component. ↩

  5. React docs, Children. ↩ ↩2

Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.