-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Feature: Dynamic ref: names #881
Comments
The alternative we'd discussed was to create an array of refs, so this... {{#each items as item}}
<Subcomponent ref:item></Subcomponent>
{{/each}} ...would result in this: console.log(component.refs.items); // [Subcomponent, Subcomponent, Subcomponent] The advantage of that approach is that you can do I wonder if there's a third option worth considering: {{#each items as item, i}}
<Subcomponent ref:items[i]></Subcomponent>
{{/each}} In that case, we're explicitly opting in to One wrinkle — we'd probably want {{#each items as item}}
<Subcomponent ref:items[item.name]></Subcomponent>
{{/each}} Not sure how best to handle that distinction. |
I personally think the ideal is to have an explicit reference in the code, without an implicit name translation. So, IMO, I like the 2nd & 3rd options that you presented. {{#each items as item, i}}
<Subcomponent ref:items[i]></Subcomponent>
{{/each}} {{#each items as item}}
<Subcomponent ref:items[item.name]></Subcomponent>
{{/each}} It seems like Another factor to consider is supporting a n-level hierarchy... {{#each parents as parent, i}}
{{#each items as item, j}}
<Subcomponent ref:parents[i].items[j]></Subcomponent>
{{/each}}
{{/each}} {{#each parents as parent}}
{{#each items as item}}
<Subcomponent ref:parents[parent.name].items[item.name]></Subcomponent>
{{/each}}
{{/each}} |
There might be some value in the React perspective on refs, which is: deprecate named refs, and have all refs be functions which are passed the element, and which do whatever the developer wants with it. In Svelte, these could be sort of an 'on create' type of event handler, and which would have access to the element/component instance (via some special name) and which also has access to all of the values on the scope at that point (including, for example, each block variables or indexes). |
Any update on this settings ref to dynamically rendered components? |
|
I'd like to reference a component in an #each block. To do so, I propose a feature to dynamically set the name of the ref using escaped variables. Is this a good idea? Is there a better alternative?
The text was updated successfully, but these errors were encountered: