Skip to content
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

docs: svelte 5 event handler pass variables? #11671

Closed
MentalGear opened this issue May 17, 2024 · 2 comments · Fixed by #11801
Closed

docs: svelte 5 event handler pass variables? #11671

MentalGear opened this issue May 17, 2024 · 2 comments · Fixed by #11801

Comments

@MentalGear
Copy link

Describe the problem

https://svelte-5-preview.vercel.app/docs/event-handlers

The docs do not describe how to pass variables with the event from a subComponent to overlying component.

Describe the proposed solution

Describe in the docs.

Importance

would make my life easier

@dummdidumm
Copy link
Member

I don't know what exactly you mean. Can you clarify? (It sounds like you mean https://svelte-5-preview.vercel.app/docs/event-handlers#bubbling-events, which is already described)

@dummdidumm dummdidumm added the awaiting submitter needs a reproduction, or clarification label May 17, 2024
@jamesst20
Copy link

jamesst20 commented May 17, 2024

@dummdidumm

While it's pretty easy to guess how to pass parameters using events, I kind of agree there is no "recommended" approach in the doc.

I believe what @MentalGear is trying to say (it's just a guess) is that previously using Svelte 4

<script>
	import { createEventDispatcher } from 'svelte';

	const dispatch = createEventDispatcher();
</script>

<button on:click={() => dispatch('notify', 'detail value')}>Fire Event</button>

You could pass the value in the dispatch method ('detail value') and you would fetch it inside the event detail property

<script>
	function callbackFunction(event) {
		console.log(`Notify fired! Detail: ${event.detail}`);
	}
</script>

<Child on:notify={callbackFunction} />

Now with Svelte 5, there is no "documented" way on how you pass custom values and you also lose the "event" unless you pass it yourself.

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

	let count = $state(0);

	function handleClick(e) {
                count++;
                onclick?.(count);
	}
</script>

<button onclick={handleClick}>
	clicks: {count}
</button>
<script>
	function callbackFunction(count) {
		console.log(`Click fired! New value: ${count}`);
	}
</script>

<Child onclick={callbackFunction} />

dummdidumm added a commit that referenced this issue May 28, 2024
- clarify that a callback prop means passing a function
- enhance the example so that a parameter is passed and the prop is explicitly invoked to visualize the concept better

closes #11671
@dummdidumm dummdidumm added documentation and removed awaiting submitter needs a reproduction, or clarification labels May 28, 2024
dummdidumm added a commit that referenced this issue May 28, 2024
- clarify that a callback prop means passing a function
- enhance the example so that a parameter is passed and the prop is explicitly invoked to visualize the concept better

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

Successfully merging a pull request may close this issue.

3 participants