Skip to content

Ability to get reference of current component #6028

Open
@janproch

Description

@janproch

Is your feature request related to a problem? Please describe.
Add ability to get reference of current component. Something like bind:this, but bind:this works only for child component

Use case:
I need to register active component to global handler. I have global menu, there is eg. command "Refresh" and this command should call "export function refresh() {}" of current component

Describe the solution you'd like
Probably the best way would be some API, like getCurrentComponent(), similar to getContext() API.

Describe alternatives you've considered
There are two workarounds:
Bind in parent component and pass down in prop:

<script>
  let activeComponent;
</script>

<Child instance={activeComponent} bind:this={activeComponent} />

Or a bit tricky solution using svelte:self
See this REPL: https://svelte.dev/repl/3b32cfb5331843b286f02e2bb28a294a?version=3.33.0

How important is this feature to you?
There are workarounds, but if would be nice to be able to do this directly

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions