sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
78.19k stars 4.09k forks source link

State changes in onMount fail to trigger afterUpdate in child components #5593

Open rdmurphy opened 3 years ago

rdmurphy commented 3 years ago

Describe the bug This may be by design, but I couldn't tell — when state changes are made within onMount (that would cause a component to update), these changes will not trigger afterUpdate within any child components. It appears you can only depend on afterUpdate triggering if a state change happens outside of onMount.

To Reproduce https://svelte.dev/repl/a3a7cb61eb7b4b74aed5eb1340fd0c33?version=3.29.4

I would expect the afterUpdate logs within Canvas.svelte to reflect the changes made in onMount within App.svelte.

Expected behavior Ideally all state/component changes would propagate. It'd make sense I think if in this scenario afterUpdate triggers twice.

Information about your Svelte project:

Severity It was certainly a surprise! I know it's possible to hack around this with tick() (though I'm unsure if it makes more sense to do that in onMount or afterUpdate) and that may ultimately be "the answer."

dummdidumm commented 3 years ago

For me it would be unexpected if it would trigger another afterUpdate. I see onMount as some kind of initialisation method, that triggering other hooks depending on it's content seems inconsistent to me. Having an Angular background onMount is similar to ngOnInit to me, and that doesn't trigger ngOnChange either.

stale[bot] commented 3 years ago

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.

stale[bot] commented 2 years ago

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.

rmunn commented 2 years ago

The Svelte docs say that the first time afterUpdate runs will be after the initial onMount, but that applies to lifecycle functions run within the same component. The docs say nothing about what order lifecycle functions will run in parent and child components, so it's not clear to me what the expected behavior is. So I'm not sure if this is a bug, or working as designed.

P.S. The behavior in current Svelte is unchanged: https://svelte.dev/repl/a3a7cb61eb7b4b74aed5eb1340fd0c33?version=3.46.4