Skip to content

Cannot reassign or bind to snippet parameter. #12688

Answered by brunnerh
leandergangso asked this question in Q&A
Discussion options

You must be logged in to vote

You cannot bind to destructured primitive arguments and you have to pass in a state object that you can reference, so one of its properties can be bound. E.g.

<!-- Component -->
<script lang="ts">
	import { onMount } from 'svelte'
	interface Props {
		trigger: Snippet<[{ refElem: HTMLElement }]>
	}
	let { trigger }: Props = $props()
	let obj = $state({ ref: null });
	onMount(() => {
		console.log(obj.ref ? "ok" :"no")
	})
</script>

{@render trigger(obj)}
<!-- App.svelte -->
<script lang="ts">
	import Component from './Component.svelte'
</script>

<Component>
	{#snippet trigger(obj)}
		<button bind:this={obj.ref}>my btn</button>
	{/snippet}
</Component>

REPL

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@leandergangso
Comment options

Answer selected by leandergangso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants