Skip to content

Transition-group animates only last element if using index as the key #8718

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 27, 2018 · 10 comments
Closed

Comments

@megacromulent
Copy link

megacromulent commented Aug 27, 2018

Version

2.5.17

Reproduction link

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

Steps to reproduce

  1. Create vue app with items displaying in a list.
  2. Add transition group with v-for with format like v-for="(item, index) in items"
  3. Use "index" from v-for as the v-bind:key (like: v-bind:key="index" )
  4. Add and remove items from the data.

What is expected?

The item being removed/added should have the transition applied to it.

What is actually happening?

The last item in the list gets the transition applied.


This may be an old bug, as I was hunting for a solution to my problem, I ran into this from May 2017:

https://stackoverflow.com/questions/43598308/vue-js-wrong-transition-group-animation-position

If this is the intended behavior using index as the key, then can their be at least a warning message in the dev version of Vue.js to explain this? Or perhaps searchable text in the documentation with something like "only last item in list is animating/transition applied" so a solution can be found without thinking there is a bug in Vue.js.

@posva
Copy link
Member

posva commented Aug 28, 2018

This is expected, the key controls how Vue reuses elements/components and is therefore crucial for transition-group. You can read more about it in the guide 🙂

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

megacromulent commented Aug 29, 2018

I respect your time, so please understand that I didn't put this bug report up without looking for an answer first, so please be respectful of my time and don't be so dismissive.

I found a new error as well during this search (see example at end).

Search of documentation
I searched the documentation entirely, either this is a bug, or it's undocumented, or it's not documented in a reasonable place. I did a search for index and transition on the guide, and looked through every single instance I could find.

You can read more about it in the guide

I know this is unreasonable (usually) to ask, but I seriously looked through everything, even searching online, and found nothing on this topic.

Can you provide a link to this documentation?

Search results
Index shows up 12 times on the transitions guide. There is no mention of the effects index has on transitions on the transitions section
https://vuejs.org/v2/guide/transitions.html

Index shows up 19 times on list rendering guide, but no mention of transitions.
https://vuejs.org/v2/guide/list.html

Index shows up 7 times on the API docs, none of the reference or are in context of the transition-group or transitions.
https://vuejs.org/v2/api/#transition-group

Updated example with new error found:
I found one example in the cookbook using index in the :key and it fails worse than the other example.
https://vuejs.org/v2/cookbook/serverless-blog.html#Display-posts

Instead of just the last item animating, the entire block fades in/out. (3rd example)
https://codepen.io/megacromulent/pen/wEzWNL

@posva
Copy link
Member

posva commented Aug 29, 2018

Using the index as the key is the default behaviour. If you have propositions about notes to add to the docs, you should open a Pull Request with your proposal on the vuejs.org repository 🙂. Maybe a not on transition-group page is the right place
for the last example, all the keys are changing so Vue is replacing every single li.

Remember you can use the forum and the Discord server to get quick feedback on questions 🙂

@Justineo
Copy link
Member

Justineo commented Aug 29, 2018

@megacromulent

From Vue's perspective, key is used to identify unique items in a list.

Let me give you an example: assume we have ['a', 'b', 'c', 'd'] and rendered a list out of this array using v-for and keyed the items with their indexes. When you remove 'b' from the list, you are telling Vue that item 1(key) is now 'c', item 2 is now 'd' and item 3 is now removed. As transition occurs when an element/component got added/removed/showed/hide from the view, only the last item will be transitioned out.

@megacromulent
Copy link
Author

megacromulent commented Aug 29, 2018

@Justineo

Thanks for the explanation, that certainly clears up why the errors are happening.

But the results (which look like errors) are not documented anywhere that I could find, and if they are, not easily found.

@posva

...open a Pull Request with your proposal on the vuejs.org repository...

I didn't even consider this idea, so I will do that. I've never done a PR before, so I hope I don't make more of a mess.

Thanks guys.

Edit:

What about the error in the Vuejs.org cookbook?

@davidgolden
Copy link

+1 Also experiencing this issue. Switched to different keys and it works. @megacromulent I've also never done a PR and would be interested in collaborating if you want a second pair of eyes.

@megacromulent
Copy link
Author

megacromulent commented Oct 9, 2018 via email

@ggaabe
Copy link

ggaabe commented Oct 28, 2018

Was getting this issue as well. I'm creating a dynamic list where every new item added is blank and so there are multiple duplicates. Tried using the item as the key but get a warning not to use non-primitives. The v-move transition works on creation but not deletion of items.

@NE-SmallTown
Copy link

NE-SmallTown commented Dec 10, 2018

The pr #8748 doesn't give any explaination which seems not reasonable. The problem is that the key rule/problem appears everywhere, not just <transition-group>, we should just give a warning rather than error because in some situations we will not change the order of the array/list. Just throw error like now is opinionated

@Rabbitzzc
Copy link

+1. This needs to be represented by a unique key. At the same time, when the data changes, can you ensure that the key is still unique after re-rendering.

Delwin444 added a commit to Delwin444/fix-cost-calculator that referenced this issue Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants