@@ -45,6 +45,7 @@ export default function useScrollTo<T>(
45
45
offset : number ;
46
46
originAlign : ScrollAlign ;
47
47
targetAlign ?: 'top' | 'bottom' ;
48
+ lastTop ?: number ;
48
49
} > ( null ) ;
49
50
50
51
// ========================== Sync Scroll ==========================
@@ -63,6 +64,7 @@ export default function useScrollTo<T>(
63
64
const height = containerRef . current . clientHeight ;
64
65
let needCollectHeight = false ;
65
66
let newTargetAlign : 'top' | 'bottom' | null = targetAlign ;
67
+ let targetTop : number | null = null ;
66
68
67
69
// Go to next frame if height not exist
68
70
if ( height ) {
@@ -73,7 +75,7 @@ export default function useScrollTo<T>(
73
75
let itemTop = 0 ;
74
76
let itemBottom = 0 ;
75
77
76
- const maxLen = Math . min ( data . length , index ) ;
78
+ const maxLen = Math . min ( data . length - 1 , index ) ;
77
79
78
80
for ( let i = 0 ; i <= maxLen ; i += 1 ) {
79
81
const key = getKey ( data [ i ] ) ;
@@ -102,9 +104,6 @@ export default function useScrollTo<T>(
102
104
}
103
105
104
106
// Scroll to
105
- let targetTop : number | null = null ;
106
- let inView = false ;
107
-
108
107
switch ( mergedAlign ) {
109
108
case 'top' :
110
109
targetTop = itemTop - offset ;
@@ -120,16 +119,16 @@ export default function useScrollTo<T>(
120
119
newTargetAlign = 'top' ;
121
120
} else if ( itemBottom > scrollBottom ) {
122
121
newTargetAlign = 'bottom' ;
123
- } else {
124
- // No need to collect since already in view
125
- inView = true ;
126
122
}
127
123
}
128
124
}
129
125
130
126
if ( targetTop !== null ) {
131
127
syncScrollTop ( targetTop ) ;
132
- } else if ( ! inView ) {
128
+ }
129
+
130
+ // One more time for sync
131
+ if ( targetTop !== syncState . lastTop ) {
133
132
needCollectHeight = true ;
134
133
}
135
134
}
@@ -140,12 +139,13 @@ export default function useScrollTo<T>(
140
139
...ori ,
141
140
times : ori . times + 1 ,
142
141
targetAlign : newTargetAlign ,
142
+ lastTop : targetTop ,
143
143
} ) ) ;
144
144
}
145
145
} else if ( process . env . NODE_ENV !== 'production' && syncState ?. times === MAX_TIMES ) {
146
146
warning (
147
147
false ,
148
- 'Seems `scrollTo` with `rc-virtual-list` reach toe max limitation. Please fire issue for us. Thanks.' ,
148
+ 'Seems `scrollTo` with `rc-virtual-list` reach the max limitation. Please fire issue for us. Thanks.' ,
149
149
) ;
150
150
}
151
151
} , [ syncState , containerRef . current ] ) ;
0 commit comments