-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why getCurrentInstance
is descriped as anti pattern in application code?
#1422
Comments
getCurrentInstance
is descriped as anti pattern in applikation code?getCurrentInstance
is descriped as anti pattern in application code?
Because the instance is an internal instance that exposes non-public APIs. Anything you use from that instance can technically break between any release types, since they are not subject to semver constraints. I'm not sure why you need the setup context in nested composables, but explicitly passing arguments to composables make them less coupled to the consuming component, thus easier to understand and test in isolation. In general a library designed to work with Composition API should expose special variables via its own composables (e.g. |
Ahh thanks for your response and clarification. Yeah the nested composables doesn't make much sense. We are trying to keep component setup code as short as possible. So we have a lot of code like this: // setup example
return useCustomerTable(props, context); This is where a lot of nested composables come from |
Re:
Quote:
As a long-time Vue2+Vuex developer recently switching to Vue3+Pinia for a new project, the Assuming you can do everything that you are able to do with the options API (plus more), it seems like a good decision. However, when interfacing with VanillaJS libraries, I often use But, if I found a post on Stack Overflow that said you can put a template ref on the root element of your component. Is that the new convention for accessing what used to be Thanks in advanced! UPDATE |
that is very strange advice from documentation.. What if i want gradually migrate from options api to composition api. And what if i have only vue 2.7 and bunch of legacy code and old libraries, that don't have composition api possibilities - but that libraries required to bypass rewriting all legacy code that rely on them. All what i can do is using getCurrentContext as a glue between composition api to options api to access some this. If getCurrentInstance is not recommended , than might be introduced good way to access component instance (like in options api) to composition api for the sake of gradual refactoring a lot of legacy code |
I really dont understand why
getCurrentInstance
is descripted as anti pattern in application code.Here is the documentation part that i mean:
docs/src/api/composition-api.md
Lines 216 to 223 in 10ed910
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 needSetupContext
. Currently we are passing the context, but in my option it makes refactoring hard and i think it would be a great improvment to usegetCurrentInstance
in some cases.Whats the reason its descripted as anti pattern?
SetupContext
Type doens't get extended?For example a nested hook needs some vuetify variable of
SetupContext
or use of a Router without Provide / Inject APIThe text was updated successfully, but these errors were encountered: