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:
What I think is happening is that instantiating the component is immediately running the $: reactive code, which dispatches the event synchronously, before the parent component attaches the listener. I'm currently leaning towards this being something to document rather than being something to change.
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 onMount is fired. Thanks to that I have chance and time to properly initialise all the properties without reactive calls and I do not have to ignore these "initialising" events before proper initialisation.
To sum it up... Note in the docs will be fully appropriate.
My case would be resolved by adding event call manually to the onMount function.
Most helpful comment
Would it be possible to display console warning if dispatch was called before onMount or before parent attached listener to event?