-
Notifications
You must be signed in to change notification settings - Fork 546
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
Access to template refs with composition API #146
Comments
Your proposition makes sense to me. I, too, think that the current design for using DOM refs with composition apis is not intuitive. It caused me a fair bit of frustration on my first try.
|
I don't disagree on some points, but I am also unsure if one or the other is better. I personally like the ref API as it is, but I'll admit it has felt a little awkward when explaining to others. The link between That said, being able to populate reactive refs with DOM refs allows for more easily decoupling the logic around those refs. With refs on the setup context interface, you would have to pass that to composition functions that deal with refs, and I don't see any good way you could rename them. For example: setup(_, { refs }) {
const { focus /* method */ } = useFocus(refs)
return { focus }
} The problem I see with this example is that we have to look into the Also, on a couple of the points you bring up, I'm not sure the alternative works any better:
Other thoughts:
|
Fair point. The same could be said about passing a prop to a function, which is probably a lot more common than passing a ref. I think the only way to do the currently is: setup(props, { refs }) {
// Example with refs, props works in exactly the same way
const { el1, el2: renamed } = toRefs(refs);
useFocus(renamed);
} I think passing DOM elements to mixins should be an unusual case. The "better" way to compose stuff that manipulates DOM is to encapsulate the behavior in either a directive or a component. It seems to me that using the ref inside the component itself is the main use case and I wouldn't mind doing One thing that annoys me a lot is that there would be no strong typing, unlike props. Unless you declare refs in some way, the best you could do is Typing is a big drawback :( |
The newly introduced |
Hi,
I was playing around with Composition API and IMHO access to template refs is confusing, slightly complicated and might cause unexpected errors in the code.
1. Problem
Currently to use template refs I have to:
Problems with this approach:
1.) Without type annotation this line isn't in any way different from regular
refs
used to store reactive data:const input = ref(null);
.2.) Name of the variable must match
ref=""
in the template. I personally like to addElement
(eg.inputElement
) to variable names to indicate that they are DOM elements.3.) I have to look into both
setup
method and template to know that this is a reference to DOM element.4.) When
ref=""
in the template is renamed, the name of the variable must also be updated.5.) There might a case when someone tries to use
ref
with the same name asref=""
in the template and assign some value to it - Vue will keep overriding it to the DOM element.2. Possible solution
In Vue 2 with
@vue/composition-api
plugin it's possible to access template refs from context like so (TypeScript will throw an error, becauserefs
doesn't exists onSetupContext
interface, however it works fine):However the same property doesn't exist in Vue 3 (SetupContext interface).
I think that this approach is much better, but I'd like to know your opinion.
The text was updated successfully, but these errors were encountered: