Skip to content

Commit 14faf2a

Browse files
fix(RecycleScroller): Introduce an item wrapper to reduce re-render (#742)
Co-authored-by: Guillaume Chau <guillaume.b.chau@gmail.com>
1 parent b7c0e52 commit 14faf2a

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- Avoir re-renders of slots -->
2+
3+
<template>
4+
<component
5+
:is="itemTag"
6+
class="vue-recycle-scroller__item-view"
7+
>
8+
<slot
9+
:item="view.item"
10+
:index="view.nr.index"
11+
:active="view.nr.used"
12+
/>
13+
</component>
14+
</template>
15+
16+
<script>
17+
export default {
18+
props: {
19+
view: Object,
20+
itemTag: String,
21+
},
22+
}
23+
</script>

packages/vue-virtual-scroller/src/components/RecycleScroller.vue

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
class="vue-recycle-scroller__item-wrapper"
2727
:class="listClass"
2828
>
29-
<component
30-
:is="itemTag"
29+
<ItemView
3130
v-for="view of pool"
3231
ref="items"
3332
:key="view.nr.id"
33+
:view="view"
34+
:item-tag="itemTag"
3435
:style="ready
3536
? [
3637
(disableTransform
@@ -54,12 +55,10 @@
5455
mouseleave: () => { hoverKey = null },
5556
}"
5657
>
57-
<slot
58-
:item="view.item"
59-
:index="view.nr.index"
60-
:active="view.nr.used"
61-
/>
62-
</component>
58+
<template #default="props">
59+
<slot v-bind="props" />
60+
</template>
61+
</ItemView>
6362

6463
<slot
6564
name="empty"
@@ -88,13 +87,15 @@ import { getScrollParent } from '../scrollparent'
8887
import config from '../config'
8988
import { props, simpleArray } from './common'
9089
import { supportsPassive } from '../utils'
90+
import ItemView from './ItemView.vue'
9191
9292
let uid = 0
9393
9494
export default {
9595
name: 'RecycleScroller',
9696
9797
components: {
98+
ItemView,
9899
ResizeObserver,
99100
},
100101
@@ -205,6 +206,9 @@ export default {
205206
pool: [],
206207
totalSize: 0,
207208
ready: false,
209+
/**
210+
* We need the key of the hovered item to prevent ItemView that gets recycled to keep the hover state.
211+
*/
208212
hoverKey: null,
209213
}
210214
},

0 commit comments

Comments
 (0)