-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Callback refs as additional alternative to "named" refs #5987
Comments
FYI this feature has been dropped at the time because of this bug #4998 |
I am not very sure what you preferred, but i think |
Thanks a lot, no1xsyzy, for that information and your demo. That's why Facebook/React have switched from 'string refs' to 'callback ref': Moreover in Vue it's not possible to use refs in templates for functional components (as that would require callback refs). In some cases and only for non-functional component it may be possible to simulate the behavior of callback refs by using unique generated ref names and some additional logic in certain lifecycle methods - but that would be really ugly, in some cases slow and not that powerful. The feature to allow 'ref callback' would open Vue some really interessting new possibilites. |
I changed my mind regarding the above suggested signature of the callback function: Instead of I would now suggest: Means "myRefCallback" will either be called: React does not have that second argument in the ref callbacks, but the first argument would behave exactly like in React. |
I'm also interested in Vue implementing callback refs. They're so much more powerful in my opinion. In my situation, as a workaround, I've resorted to storing a reference to the Unfortunately I have no way of knowing when the instance has been created/mounted, because until then |
Frankly speaking I don't think that it's a good idea. Function as a So, eventually it may have performance issues |
Maybe we can use directive to implement ref callback function. |
I really don't know why is this issue is neglected. |
That's a pretty disingenuous argument. Just because you can do bad things with the feature doesn't mean the feature is bad. You could easily create infinite update cycles with get/set refs, but that doesn't mean we don't support it. In addition, "crazy side effect stuff" over-generalizes an entire class of necessary features. The whole point of UI libraries is to manage side effects, otherwise what's the point of building UIs? It's the reason why we need |
I'm building a library for Vue 3 and this is one of the limiting factors. I have controlled components that can receive an <Dialog.Content>
...
</Dialog.Content>
<!-- becomes -->
<div id="ally-0-content" role="dialog" aria-modal="true" aria-labelledby="ally-0-title" aria-describedby="ally-0-description" data-state="open">
...
</div>
<Dialog.Content as-child v-slot="props">
<section v-bind="props">
...
</section>
</Dialog.Content>
<!-- becomes -->
<section id="ally-0-content" role="dialog" aria-modal="true" aria-labelledby="ally-0-title" aria-describedby="ally-0-description" data-state="open">
...
</section> However, the controlled component sometimes needs a reference to the DOM node to implement stuff like focus trapping, custom event management, layout measurements etc. If rendering as a fragment, there's no official way to get the DOM node that's supposed to receive the props, but a callback ref passed via WorkaroundsSurprisingly, when I simply pass the callback ref as a property of the slot props, the callback ref works as expected. While this solves my issue, I'm not sure if this is intended behaviour or if it will be removed in the future by a fix.
|
What problem does this feature solve?
Currently if you want to use refs to elements (DOM elements or Vue components - see attribute/prop "ref") you have to provide a string as "ref" attribute. A callback function as "ref" attribute like in React is currently not supported in Vue.
It would be great if also callback functions could be provided as "ref" attributes (especially when doing a bit more advanced stuff using the "render" function).
The callback function should be called both when the referred element is created and also when it is disposed (React for example passes null in the latter case).
It seems that this feature had already been implemented in the past but has been reverted later (I do not know for what reasons the changes have been reverted) => see: "[WIP] Support for ref callback #4807"
Thank you very much.
What does the proposed API look like?
Please see line 178 here:
90c6c29
const myRefCallback(ref, remove) {...} (where "remove" is boolean) seems to me like a better solution that the one that is used in React where in the "remove" case the ref callback function is just called with null.
The text was updated successfully, but these errors were encountered: