Skip to content

[Svelte 5] bind:this on components not working #10026

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
terrywh opened this issue Dec 29, 2023 · 8 comments
Closed

[Svelte 5] bind:this on components not working #10026

terrywh opened this issue Dec 29, 2023 · 8 comments

Comments

@terrywh
Copy link

terrywh commented Dec 29, 2023

Describe the bug

'undefined' is logged with the following:

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE41QwUrFMBD8lXXx8B6U9nmtbUHwL4wHX5raYJoNyfYVCfl32Rb0IIK3nWVmZ3YyTtaZhO1LRv-2GGzxKQSskD-DgHQzjg1WmGiNWjZd0tEGHpRXbJdAkeHZLARTpAUU1o2g-tApfBSaMwzGyV6gVzytXrMlD2mm7XSGLEvFmnwiZ2pH76eDf971Rfmu-bH13Whvu3-3O2925LlX-HC5KISr9WPLs019Pm6UodszHYrrykweyGtn9UefJUEZDn-Zd1JzsAbxFS-scKHRTtaM2HJcTam-65LT_-1LisggQugVbhTdqBAK9HAfIoV0kn9_vzob56iCLMJy91ek1_IF6Flw4s0BAAA=

Reproduction

  • app.svelte
<script>
	import Demo from "./Demo.svelte";
	let elDemo;

	function show() {
		console.log(elDemo);
	}
</script>

<div>
	<Demo width="100" bind:this={elDemo}></Demo>
	<button onclick={show}>
		show
	</button>
</div>
  • Demo.svelte
<script>
	let { name ="world" } = $props();
</script>

<div>hello, {name}!</div>

Logs

undefined xN

System Info

MacOS/Chrome

Severity

blocking an upgrade

@dm-de
Copy link

dm-de commented Dec 29, 2023

EDIT!
I changed my mind.

I'm not sure...
but if you wanted this:

LINK

Note that i changed bind:this to bind:name
and Svelte 5 forbids default value

@terrywh
Copy link
Author

terrywh commented Jan 1, 2024

I'm binding the component element, for method / property access. are you suggesting I should bind every method/props?

@Malix-Labs
Copy link

Malix-Labs commented Jan 2, 2024

This could be the consequence of #9764

@trueadm
Copy link
Contributor

trueadm commented Jan 2, 2024

You're binding to a component that doesn't export anything. You're going to get undefined. This is listed in the expected breaking changes for Svelte 5 – previously you'd get an instance of a component, but we don't do that anymore in Svelte 5.

@trueadm trueadm closed this as completed Jan 2, 2024
@dm-de
Copy link

dm-de commented Jan 3, 2024

I want to share an example with Svelte 5 bind:this

LINK

@terrywh
Copy link
Author

terrywh commented Jan 3, 2024

You're binding to a component that doesn't export anything. You're going to get undefined. This is listed in the expected breaking changes for Svelte 5 – previously you'd get an instance of a component, but we don't do that anymore in Svelte 5.

then access to the props should use bind like the example @dm-de provided above ( bind:name={xxx} ) or using <svelte:options accessor={true} />

@Florinstruct
Copy link

What dm-de proposed is perfectly fine for Legacy Svelte, but not for Svelte 5.

@xreider
Copy link

xreider commented Dec 7, 2024

The only working solution that does not throw an error. Note that a function is passed that assigns a node
https://svelte.dev/playground/f2129e9f3e1d4910a0112e9c6fa47441?version=5.8.0

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

No branches or pull requests

6 participants