Sending DOM reference to parent from child #15178
Answered
by
brunnerh
karchung0930
asked this question in
Q&A
-
I wanna send the reference of an element to its parent once it's ready ( mounted ). In Svelte 4, my thought is to use the In Svelte 5, may I know what is the best way of doing this? Child:
Parent:
|
Beta Was this translation helpful? Give feedback.
Answered by
brunnerh
Feb 1, 2025
Replies: 1 comment
-
I would just make it a regular (bindable) property. E.g. <!-- Button.svelte -->
<script>
let {
element = $bindable(),
children,
...rest
} = $props();
</script>
<button bind:this={element} {...rest}>
{@render children?.()}
</button> <script>
import Button from './Button.svelte';
let element = $state();
</script>
<Button
bind:element
type="button"
onclick={() => alert(element.textContent)}
>
Hi
</Button> The element will be available in the parent |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
karchung0930
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would just make it a regular (bindable) property. E.g.
Playground
The element will be available in the parent
onMount
, as is the case in the component itself.