Skip to content

Svelte 5: Can't set default snippet props #9878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Not-Jayden opened this issue Dec 9, 2023 · 3 comments · Fixed by #9898
Closed

Svelte 5: Can't set default snippet props #9878

Not-Jayden opened this issue Dec 9, 2023 · 3 comments · Fixed by #9898
Assignees

Comments

@Not-Jayden
Copy link
Contributor

Describe the bug

A method that was working previously to ensure a snippet prop function will always be defined was by setting the snippet prop with a default snippet defined inside the current component.

i.e.

<script>
	let {children = fallback} = $props();
</script>

{#snippet fallback()}
	Fallback
{/snippet}

<button>
	{@render children()}
</button>

This seems to have stopped working though, potentially with the changes in #9759

Can still conditionally check the children prop is defined as a workaround for setting a fallback, but I do personally prefer being able to default it, given fallback and children should exist in the same scope anyway.

Reproduction

REPL

Logs

Error: `Cannot access 'fallback' before initialization`

System Info

Svelte REPL

Severity

annoyance

@dummdidumm
Copy link
Member

Possible solution: unshift the snippet instead of pushing it to the init array, that way it comes first

@PP-Tom
Copy link

PP-Tom commented Dec 11, 2023

I would personally do:

<script>
	let { children } = $props();
</script>

<button>
	{#if children}
		{@render children()}
	{:else}
		Fallback
	{/if}
</button>

Would this not work for you?

@Not-Jayden
Copy link
Contributor Author

I would personally do:

<script>
	let { children } = $props();
</script>

<button>
	{#if children}
		{@render children()}
	{:else}
		Fallback
	{/if}
</button>

Would this not work for you?

Works fine, I already mentioned that as an alternative in the description. Just reporting a regression of functionality that worked previously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants