# In Svelte 5, children is a function

- Published: 2026-11-28
- Tags: Why Svelte, Svelte
- URL: https://devtrillo.com/writing/why-svelte-7-children-is-a-function/

> Part 7 of a series on Svelte in 2026. [Part 6](/writing/why-svelte-6-props-are-getters/) 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:

```txt
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.[^snippet]

## 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>`:

```svelte
{#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:

```svelte title="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):

```js title="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:[^snippet]

```svelte title="App.svelte"
<List {items}>
  <strong>My list</strong>

  {#snippet item(text)}
    <span>{text.toUpperCase()}</span>
  {/snippet}
</List>
```

```svelte title="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:

```svelte title="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.[^changelog]

## 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.[^migration] 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:

**From slots to snippets.** In Svelte 4 I used…

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

**Quiz:** 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?

- 8
- 2
- 6

**Answer:** 2 — Once per row, at mount. The arguments are getters and count is read inside an effect, so later changes only update the text nodes.

- Not "8": That's React: the render prop is called for each row on each of the 4 renders. A Svelte snippet isn't called again when its inputs change.
- Not "6": That's 2 rows times 3 changes, as if each change re-ran the snippet. It doesn't. Only the text that reads count updates.

## 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`."[^passing] 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."[^children]

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."[^children] 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.[^snippet]

**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](https://svelte.dev/docs/svelte/snippet) page first; it's short and covers scope, typing and exports.[^snippet] The "Snippets instead of slots" section of the [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Snippets-instead-of-slots) has the slot-by-slot diffs and the maintainers' reasons for the change.[^migration]

Next: shared state in `.svelte.ts` modules.

[^snippet]: Svelte docs, [`{#snippet ...}`](https://svelte.dev/docs/svelte/snippet) and [`{@render ...}`](https://svelte.dev/docs/svelte/@render).

[^migration]: Svelte docs, [Svelte 5 migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Snippets-instead-of-slots).

[^changelog]: [Svelte changelog](https://github.com/sveltejs/svelte/blob/main/packages/svelte/CHANGELOG.md), 5.5.0: "allow snippets to be exported from module scripts" ([#14315](https://github.com/sveltejs/svelte/pull/14315)).

[^passing]: React docs, [Passing Props to a Component](https://react.dev/learn/passing-props-to-a-component#passing-jsx-as-children).

[^children]: React docs, [`Children`](https://react.dev/reference/react/Children).

- **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
