Skip to content

Current component should (probably?) not be available inside onMount #4259

@Conduitry

Description

@Conduitry

Describe the bug
Things that internally use get_current_component() should probably fail when they are called from an onMount callback.

Logs
None.

To Reproduce

<script>
	import { getContext, onMount } from 'svelte';
	import { get_current_component } from 'svelte/internal';
	onMount(() => {
		console.log(get_current_component());
	});
</script>

Expected behavior
I'd expect this to crash, but it doesn't. There is an exception if you use setTimeout instead of onMount.

Stacktraces
n/a

Information about your Svelte project:

  • Svelte 3.17.0

Severity
I'm not sure. I'm not actually positive this is a bug, but it feels like it. I don't think we want people to be able to call other lifecycle hooks or access the context within onMount.

Additional context
Came up in chat by way of a question about calling setContext in a component and then calling getContext during that component's onMount callback. It feels like this shouldn't actually be allowed.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions