-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5: using bind:this
in $derived
#13181
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
Comments
If you want to use it in a derived it has to be state <script lang="ts">
let b: HTMLParagraphElement = $state();
const b2 = $derived(b?.children);
$inspect(b2);
</script>
<p bind:this={b}></p> this works fine (and the error that you are seeing on b is just about the intersection between |
My bad and thank you. Maybe this should be a documentation issue? // HTMLElement | undefined
let article = $state<HTMLElement>();
// HTMLCollection | undefined
const children = $derived(article?.children); |
What's exactly the documentation issue? |
Providing an example of Since the following is valid in Svelte 5 runes, I honestly didn't thought using <script lang="ts">
let div: HTMLDivElement;
</script>
<div bind:this={div}></div> Going one step further, maybe forcing In the example code, <script>
import { onMount } from 'svelte';
/** @type {HTMLCanvasElement} */
let canvasElement;
onMount(() => {
const ctx = canvasElement.getContext('2d');
drawStuff(ctx);
});
</script>
<canvas bind:this={canvasElement} /> Forcing <script lang="ts">
// HTMLDivElement | undefined
let div = $state<HTMLDivElement>();
</script>
<div bind:this={div}></div> |
As i've said technically you don't really need <script lang="ts">
let a;
const a2 = $derived(a.children);
</script>
<button onclick={()=>{
console.log(a2);
}}>
log child
</button>
<p bind:this={a}></p> this doesn't cause any problems because you are accessing However i agree that we need specific documentation around this topic so i'll keep this open with the doc label |
Describe the bug
Reproduction
See above.
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: