-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Event is not dispatched after component is loaded #4470
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
What I think is happening is that instantiating the component is immediately running the |
Would it be possible to display console warning if dispatch was called before onMount or before parent attached listener to event? |
Hi, I've been struggling with issue for a while now and was wondering whether I'm doing anything wrong on my part. Is there a work-around for this, since the behaviour might not be changed. I've reproduced, in a very simple way, what I would like it to do: https://svelte.dev/repl/2b0b7837e3ba44b5aba8d7e774094bb4?version=3.19.1 |
I was thinking more about this issue and after reading and searching trough some use-cases in our app I think that current behaviours in more appropriate then behaviour that I was originally excepting. Now I think it's valid behaviour that reactive statements are not running until To sum it up... Note in the docs will be fully appropriate. My case would be resolved by adding event call manually to the |
@Conduitry wrote:
I suspect that is correct. (Confirmed. See logs below.) Should we update the title (since "event is not dispatched" is not quite accurate)?: Events dispatched from child component get lost because dispatched before event listeners attached in parent component I would love to see a diagram in the docs that illustrates this sequence of events in detail (issue forthcoming!)...
I'm still not quite convinced. Wouldn't Svelte be better if users didn't have to worry about events getting lost (due to a lifecycle event / component tree initialization step order that they have absolutely no control over), and didn't have to resort to kludges like Or at least provide an elegant/easy option to synchronize/postpone the timing of these things for cases where that is preferred. Maybe something like: <Child on:fill|deferUntilSelfMounts={onFill} /> , which would instruct Svelte to queue up any Or something like: <Child on:fill={onFill} svelte:deferEventsUntilSelfMounts /> or <Child on:fill={onFill} on:*|queueUntilMounted /> to queue and defer all events dispatched by Seems like that should just be the default behavior... Note: I believe there may be related confusing race condition (not technically a race condition since it's deterministic, but what do you call it?) issues related to the use of @MrSrsen wrote:
Technically, that's incorrect (see @Conduitry's explanation for the actual reason for the behavior): reactive statements are run before Inner: checking isFilled(Some text): true [called from $: reactivate statement]
Attached listener: ƒ onFill(e) {
$$invalidate(0, isFilled = e.detail);
}
Inner: onMount: NodeList [input] This also seems to confirm @Conduitry's hypothesis that listener isn't attached in parent until after
So it sounds like you would be someone who would actually prefer that the current behavior not change? I'd be curious to see a concrete example where the current behavior would actually be useful/desirable (and if so, whether the perceived problem might actually be a non-issue in those cases, or easily worked around). Your original REPL is actually a counterexample/the opposite of what you described here — a case where everything has been sufficiently initialized (the correct values for
You mean (in your initial example) calling Sure, that works, but seems unfortunate that one needs to have that duplication. Another option (which I like even better) is to use the
I'm still curious if there's a better workaround, but my personal favorite so far is the
Oops, looks like you linked to the same REPL as the OP. Did you start from their REPL, make some changes, but forget to save your changes (which would have created a new REPL URL)? Would be interested to see what you were trying to show, about what you would like it to do. |
Yeah sorry. After some time I realised that my first Svelte codebase was not the best and I think I might wrote a lot of garbage code and a lot of weird workarounds. I just want easy and intuitive solution that will be consistent. I really like yours suggestion for optional synchronising:
because it's expressive in what it will do. In any case, I don't really well understand Svelte internals so I think I just leave solving of this issue to more experienced. :) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I had a similar question, arising from an observation that using the Component API, it’s not possible to register an
Using code like this, if I've put this in a REPL: https://svelte.dev/repl/b6704c77ec4047029658ecba1d97d027?version=3.48.0. You can see that the Component fires the same event twice, once in the initial constructor, and once after 1 second. Only the second one is seen by the callback. The comments above suggest this is expected behaviour (both using the API and the declarative syntax). If so, I think a console error if Ideally, however, the API would support adding event callbacks in the constructor. Could this ever work as a feature? |
Any progress on this one? :\ |
Is this fixed in Svelte 5? |
Since |
Yeah. I can confirm it works with 5. |
So will this bug still be present if I use Svelte 5 without runes mode? |
Nope. For me it works even without runes mode. Only svelte 5. |
|
It is not possible to dispatch events during component initialization phase (sveltejs/svelte#4470), causing errors not to be reported if generated. Storing the error and dispatching it later solves the issue.
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
Event from component is not dispatched (from reactive function) after component is loaded.
To Reproduce
https://svelte.dev/repl/2b0b7837e3ba44b5aba8d7e774094bb4?version=3.19.1
Expected behavior
Event is dispatched after component is loaded.
Information about your Svelte project:
Severity
Low, I think.
Additional context
When I call dispatch in the
setTimeout()
with zero delay then it's working corectlly.If this is desired behaviour then I think it's appropriate to write notice to documentation:
The text was updated successfully, but these errors were encountered: