Skip to content

Svelte 5: pass arguments to the children snippetΒ #9974

@oneraul

Description

@oneraul

Describe the problem

TLDR: the children snippet of a component cannot receive arguments.

Svelte 5 will deprecate slots in favour of snippets, but there is one feature of slots that I find difficult to migrate: slot props.

Imagine that Wrapper calculates some data. With props you can do (notice that data is correctly typed):

<Wrapper let:data>
    <Widget id={data.id} />
</Wrapper>

Using snippets, I cannot pass arguments to the default children snippet, so the children have to be wrapped in a named snippet.

<Wrapper>
    {#snippet withData(data: { id: string })}
        <Widget id={data.id} />
    {/snippet}
</Wrapper>

For such a simple example it looks like an anti-pattern, so I've prepared this REPL of my use case. It's a wrapper for fetching data with TanStack Query. It compares doing it without the wrapper, wrapped with slot props and wrapped with snippets. When you have many queries a wrapper like this can easily clean up a lot of code.

Describe the proposed solution

I would like to treat the children snippet as any other snippet (pass the argument to it in the render tag, inside the wrapper component), but to expose its argument as with the slot props. Example:

In Wrapper.svelte:

{@render children(data)}

Use like (the same as before):

<Wrapper let:data>
    <Widget id={data.id} />
</Wrapper>

Alternatives considered

The alternative is to wrap the children in a named snippet at every instance, or to keep using slots.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions