sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.79k stars 4.23k forks source link

Unexpected behaviour combining context API with onMount and timeouts #4984

Closed stephane-vanraes closed 4 years ago

stephane-vanraes commented 4 years ago

Describe the bug When combining slots with the context API and a specific combination of onMount and setTimeout it appears as if two instances of the same component only share 1 context.

Please refer to the REPL(s) for a demo with different combinations

Logs No logs except for 1 combo which gives getContext(...) is not a function

To Reproduce https://svelte.dev/repl/439be129b8cd4070835a8b9121e18aae?version=3.20.1 https://svelte.dev/repl/167fa53e87ef425589037ba77cf5c28d?version=3.20.1

Expected behavior The child component calls the correct contextAPI

Conduitry commented 4 years ago

Using getContext or any of the other lifecycle hook anywhere but during component initialization (i.e., synchronously from top-level code in the script block) is not supported, and it's currently a bug that calling it from within onMount doesn't immediately throw - see #4259.