Skip to content

Dev build console error when using index in :key value with v-for and transitions #8730

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
megacromulent opened this issue Aug 29, 2018 · 6 comments

Comments

@megacromulent
Copy link

megacromulent commented Aug 29, 2018

What problem does this feature solve?

Currently there is no obvious way to debug an error when including the index value in v-bind:key with v-for when using transitions. This causes erratic and unexpected behavior from transitions (and I would suspect other side effects, but these are untested).

This feature would alert the developer that they have used index value in a place where it will cause errors with transitions.

Example error:

This example should illustrate how difficult it is to determine why the v-for combined with transition-group fails.

In the example below there are 3 uses of v-for with :key. (1) one works fine, it doesn't include index in the :key value. (2) uses index as the key (ie, v-bind:key="index"). (3) uses a concatenated version (v-bind:key="item '_' index"

https://codepen.io/megacromulent/pen/gdMYLx?editors=1010

Note: this behavior was thought to be a bug because of lack of documentation on this behavior ( #8718 ) and having a console log entry would have solved the confusion instantly during development, instead of hours of debugging and reporting.

What does the proposed API look like?

Check to see if the index value is used anywhere in :key, and then check if a transition is being used, if so, then display console error, or at least a warning.

The proposal is for the dev builds only, so no additions/expansions to the API are needed for the production builds.

@posva
Copy link
Member

posva commented Aug 29, 2018

This is not possible because sometimes you may want to use the index as the key.
I really think you should open a PR to the docs to add a note in transition-group as I said in the other issue 🙂

@posva posva closed this as completed Aug 29, 2018
@megacromulent
Copy link
Author

You would never want to use index as the key with a transition because it causes an error. I demonstrated that.

Is there something wrong with my example showing this error?

@posva
Copy link
Member

posva commented Aug 31, 2018

Yeah, you are right. I don't think it makes sense to use the index in any scenario for transition-group. I'm not sure this is an easy to add warning though

@megacromulent
Copy link
Author

If I get to a point where I have more free time, I'd at least look through the code to be able to add this kind of thing.

Perhaps a dev only object that tracks features being used during a render (ie, transition is used) then when an index is found (ie, in the template render?) it would be tracked as well, and then conflicts could be found between features used.

I know it's easy to say this kind of thing, and a whole other thing to actually build it. But I am not a high level dev, so I can only theorize...

@posva posva reopened this Sep 1, 2018
HcySunYang added a commit to HcySunYang/vue that referenced this issue Sep 3, 2018
@RANGELJ
Copy link

RANGELJ commented Feb 8, 2020

In dev branch this issue is solved, why is still open?

@posva
Copy link
Member

posva commented Feb 8, 2020

It wasn't correctly reference I the commit message of the PR #8748

@posva posva closed this as completed Feb 8, 2020
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

3 participants