Skip to content

No warning for multiple refs with same name #6366

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

Closed
dsandber opened this issue Aug 14, 2017 · 9 comments
Closed

No warning for multiple refs with same name #6366

dsandber opened this issue Aug 14, 2017 · 9 comments

Comments

@dsandber
Copy link

dsandber commented Aug 14, 2017

What problem does this feature solve?

Both I and a colleague have spent hours debugging issues caused by having two refs with the same name. Two cases where this can happen:

  1. Simple stupidity:
<comp1 ref='comp'/>
<comp2 ref='comp/>
  1. Not realizing that refs don't seem to work in a v-for:
<tabs v-for='title of titles'>
<fubar v-if='title==="FUBAR"' ref='fubar'/>
</tabs>

In this case, it is reasonable to assume the ref would work correctly because there will only be one "fubar" component instantiated assuming 'titles' has no duplicates. But the ref does not work right, either because ref ignores "v-if" or because ref doesn't work right in v-for.

What does the proposed API look like?

Simply issue a Vue warning when multiple refs with the same name are encountered.

@LinusBorg
Copy link
Member

LinusBorg commented Aug 14, 2017

The second one sounds rather like

a) a mistake on your part or
b) a bug

Could you provide a small reproduction of the problem as it appears to you?


Sidenote: Are you aware that refs in a v-for end up in an array?

@LinusBorg
Copy link
Member

@nickmessing

  • About 1.: I think this should be solved in eslint-plugin-vue, instead of adding yet another warning into the core lib.
  • About 2: Would be a bug if true.

So I would remove the improvement label and ask the OP to suggest this as a rule in the repo of eslint-plugin-vue. OK with you?

@nickmessing
Copy link
Member

@LinusBorg, agree 👍

@dsandber
Copy link
Author

dsandber commented Aug 18, 2017

Disagree -- the problem is that the refs might be specified programatically, in which case no warning would be issued even though they would save the user a lot of trouble. Some possibilities:

<component :ref='calc_ref'/>  [is colon in front of ref allowed?]
<component v-if='condition1' ref='fubar'/>
<component v-if='!condition1' ref='fubar'/>

In 2) a linter would warn even though it shouldn't -- the two cases are mutually exclusive. A linter doesn't have enough information to reliably determine duplicate references -- only at run-time is there enough information.

@dsandber
Copy link
Author

dsandber commented Aug 18, 2017

Incidentally I will try to come up with a simple test case for my original second example in the next week.

@LinusBorg
Copy link
Member

I can concede that a linter will not catch dynamic $refs in many cases. For that, we would have to implement a warning mechanism.

However, a simple check does this ref already exist isn't enough either, because it's totally valid to overwrite $refs. We would somehow register all $ref assignments during the run of a render function and check for any ne assignment if it has been assigned privously in the same render run.

Will have to think about how that's feasable.

@nickmessing
Copy link
Member

@LinusBorg, there is one easy way, Proxy, we just need to wait for the drop of IE support 😅

@yyx990803
Copy link
Member

Closing due to inactivity. Please open a new issue with a reference to this one if you can follow up with more information.

@neel
Copy link

neel commented Jul 1, 2019

I also stumbled on (1). I need that, It is not an unreasonable feature.

If there can be multiple elements with the same ref then it should be possible with or without v-for. Otherwise definition of ref is unstable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants