Skip to content

Commit cec2196

Browse files
committed
warn against duplicate keys
1 parent 9db07d3 commit cec2196

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

benchmarks/reorder-list/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<script type="text/x-template" id="t">
1010
<div>
11+
<h1>{{ total }} Components</h1>
1112
<p>{{ action }} took {{time}}ms.</p>
1213
<button @click="shuffle">shuffle</button>
1314
<button @click="add">add</button>
@@ -34,13 +35,13 @@
3435
</tr>
3536
</script>
3637

37-
<h1>1000 Components</h1>
3838
<div id="el">
3939
</div>
4040

4141
<script>
42+
var total = 1000
4243
var items = []
43-
for (var i = 0; i < 1000; i++) {
44+
for (var i = 0; i < total; i++) {
4445
items.push({
4546
id: i,
4647
label: String(Math.random()).slice(0, 5)
@@ -52,6 +53,7 @@ <h1>1000 Components</h1>
5253
el: '#el',
5354
template: '#t',
5455
data: {
56+
total: total,
5557
time: 0,
5658
action: 'Render',
5759
items: items,
@@ -63,7 +65,7 @@ <h1>1000 Components</h1>
6365
}),
6466
add: monitor('add', function () {
6567
this.items.push({
66-
id: this.items.length,
68+
id: total++,
6769
label: String(Math.random()).slice(0, 5)
6870
})
6971
}),

src/runtime/vdom/patch.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import VNode from './vnode'
22
import * as dom from './dom'
3-
import { isPrimitive } from '../util/index'
3+
import { isPrimitive, warn } from '../util/index'
44

55
const emptyNode = VNode('', {}, [])
66
const hooks = ['create', 'update', 'remove', 'destroy', 'pre', 'post']
@@ -178,6 +178,12 @@ export default function createPatchFunction (modules, api) {
178178
newStartVnode = newCh[++newStartIdx]
179179
} else {
180180
elmToMove = oldCh[idxInOld]
181+
if (process.env.NODE_ENV !== 'production' && !elmToMove) {
182+
warn(
183+
'Duplicate track-by key: ' + idxInOld + '. ' +
184+
'Make sure each v-for item has a unique track-by key.'
185+
)
186+
}
181187
patchVnode(elmToMove, newStartVnode, insertedVnodeQueue)
182188
oldCh[idxInOld] = undefined
183189
api.insertBefore(parentElm, getElm(elmToMove), getElm(oldStartVnode))

0 commit comments

Comments
 (0)