Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transition-group children must be keyed...but they are keyed

Trying to use <animation-group> in my component template, but getting error:

[Vue warn]: <transition-group> children must be keyed: <div>

But I'm pretty sure that they are keyed.

//js

Vue.component('instruments', {
template: `
        <transition-group name="fade">
            <div class="instruments">
                <div class="instrument" v-for="(instrument, index) in filteredInstruments" v-bind:key="index">
                    <img v-bind:src="instrument.photo">
                    <span class="name">{{ instrument.name }}</span>
                    <span class="construction">{{ instrument.top }} / {{ instrument.backAndSides }}</span>
                    <span class="price">$ {{ instrument.price }}</span>
                </div>
            </div>
        </transition-group>
    `
}

I think that setting v-bind:key="index" would take satisfy this, but I get the error pasted above.

like image 481
Justin D Avatar asked Jan 10 '19 15:01

Justin D


2 Answers

You have to give a unique key to your <div class="instruments"> element since elements inside a <transition-group>, specifically the immediate children, are always required to have a unique key attribute.

If you don't want to give a key to .instruments, you can remove that element and assign a tag and class attributes to <transition-group> instead since it renders an actual element which by default is a <span>.

<transition-group name="fade" tag="div" class="instruments">

In this way, the warning would not appear anymore since the immediate children (.instrument) have their unique keys assigned to them.

like image 88
Ana Liza Pandac Avatar answered Jan 03 '23 23:01

Ana Liza Pandac


In some common cases.

You should check to see if there are any elements remain except all elements in for loop.

You may have an incorrect structure like this:

<transition-group name="fade">
  <div v-for="item in listItem" :key="item.id">
  </div>
  <n-button>Load more...</n-button>
</transition-group>

n-button is the cause. Should move n-button out side transition-group

like image 22
HoangYell Avatar answered Jan 03 '23 23:01

HoangYell