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

Event Handler registration & slot mounting order causes issues #9017

Open
peterkogo opened this issue Jul 21, 2023 · 0 comments
Open

Event Handler registration & slot mounting order causes issues #9017

peterkogo opened this issue Jul 21, 2023 · 0 comments
Labels
compiler Changes relating to the compiler

Comments

@peterkogo
Copy link

peterkogo commented Jul 21, 2023

Describe the bug

When there are 2 nested components

<Master>
    <Minion />
</Master>

a Minion, which uses an action that dispatches a Custom Event on mount

// Minion.svelte

<script>
    const dispatchSomeEvent = (node) => {
        // This is called on mount
        console.log('marco')
	
        const someEvent = new CustomEvent('someEvent', { bubbles: true });
        node.dispatchEvent(someEvent);
    }
</script>

<div use:dispatchSomeEvent />

and a Master, which handles the event.

// Master.svelte

<div on:someEvent={(event) => {
    // This SHOULD be called on mount of the child
    console.log('polo')
}}>
    <slot />
</div>

The console printout should be "marco" "polo" - but it is not. The Master never handles the event on initial mount.

Looking at the generated code

m(target, anchor) {
	insert(target, div, anchor);

	if (default_slot) {
		default_slot.m(div, null);
	}

	current = true;

	if (!mounted) {
		dispose = listen(div, "someEvent", /*someEvent_handler*/ ctx[2]);
		mounted = true;
	}
}

the mounting of the slot happens before the event handlers are registered.

Is there are reason for this order?

Reproduction

Link to REPL

Logs

No response

System Info

System independent

Severity

annoyance

@dummdidumm dummdidumm added the compiler Changes relating to the compiler label Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Changes relating to the compiler
Projects
None yet
Development

No branches or pull requests

2 participants