Svelte: Event is not dispatched after component is loaded

Created on 26 Feb 2020  路  4Comments  路  Source: sveltejs/svelte

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:

  • Your browser and the version: Google Chrome | 80.0.3987.87聽(Official Build)聽(64-bit)
  • Your operating system: Ubuntu 18.04.4 LTS
  • Svelte version: 3.19.1

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:

component lifecycle docs

Most helpful comment

Would it be possible to display console warning if dispatch was called before onMount or before parent attached listener to event?

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

juniorsd picture juniorsd  路  3Comments

plumpNation picture plumpNation  路  3Comments

angelozehr picture angelozehr  路  3Comments

davidcallanan picture davidcallanan  路  3Comments

bestguy picture bestguy  路  3Comments