Skip to content

Why getCurrentInstance is descriped as anti pattern in application code? #1422

Closed
@Donskelle

Description

@Donskelle

I really dont understand why getCurrentInstance is descripted as anti pattern in application code.

Here is the documentation part that i mean:

## `getCurrentInstance`
`getCurrentInstance` enables access to an internal component instance.
:::warning
`getCurrentInstance` is only exposed for advanced use cases, typically in libraries. Usage of `getCurrentInstance` is strongly discouraged in application code. Do **NOT** use it as an escape hatch to get the equivalent of `this` in Composition API.
:::

I am working on a large vue 2.6 codebase and in my optinion using getCurrentInstance can be a great improvement for reusable hooks, which need SetupContext. Currently we are passing the context, but in my option it makes refactoring hard and i think it would be a great improvment to use getCurrentInstance in some cases.

Whats the reason its descripted as anti pattern?

  • SetupContext Type doens't get extended?
  • Is it a perfomance problem?

For example a nested hook needs some vuetify variable of SetupContext or use of a Router without Provide / Inject API

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions