@@ -64,6 +64,7 @@ export default function useScrollTo<T>(
6464 const [ syncState , setSyncState ] = React . useState < {
6565 times : number ;
6666 index : number ;
67+ key ?: React . Key ;
6768 offset : ScrollOffset ;
6869 originAlign : ScrollAlign ;
6970 targetAlign ?: 'top' | 'bottom' ;
@@ -81,7 +82,11 @@ export default function useScrollTo<T>(
8182
8283 collectHeight ( ) ;
8384
84- const { targetAlign, originAlign, index, offset : rawOffset } = syncState ;
85+ const { targetAlign, originAlign, offset : rawOffset } = syncState ;
86+ const index =
87+ syncState . index >= 0
88+ ? syncState . index
89+ : data . findIndex ( ( item ) => getKey ( item ) === syncState . key ) ;
8590 const mergedAlign = targetAlign || originAlign ;
8691 const offset = getOffset ( rawOffset , { getSize, align : mergedAlign } ) ;
8792
@@ -91,7 +96,7 @@ export default function useScrollTo<T>(
9196 let targetTop : number | null = null ;
9297
9398 // Go to next frame if height not exist
94- if ( height ) {
99+ if ( height && index >= 0 ) {
95100 // Get top & bottom
96101 let stackTop = 0 ;
97102 let itemTop = 0 ;
@@ -157,12 +162,13 @@ export default function useScrollTo<T>(
157162
158163 // Trigger next effect
159164 if ( needCollectHeight ) {
160- setSyncState ( {
161- ...syncState ,
162- times : syncState . times + 1 ,
165+ setSyncState ( ( prev ) => ( {
166+ ...prev ,
167+ times : prev . times + 1 ,
168+ index,
163169 targetAlign : newTargetAlign ,
164170 lastTop : targetTop ,
165- } ) ;
171+ } ) ) ;
166172 }
167173 } else if ( process . env . NODE_ENV !== 'production' && syncState ?. times === MAX_TIMES ) {
168174 warning (
@@ -187,19 +193,22 @@ export default function useScrollTo<T>(
187193 syncScrollTop ( arg ) ;
188194 } else if ( arg && typeof arg === 'object' ) {
189195 let index : number ;
196+ let key : React . Key ;
190197 const { align } = arg ;
191198
192199 if ( 'index' in arg ) {
193200 ( { index } = arg ) ;
194201 } else {
195- index = data . findIndex ( ( item ) => getKey ( item ) === arg . key ) ;
202+ key = arg . key ;
203+ index = data . findIndex ( ( item ) => getKey ( item ) === key ) ;
196204 }
197205
198206 const { offset : rawOffset = 0 } = arg ;
199207
200208 setSyncState ( {
201209 times : 0 ,
202210 index,
211+ key,
203212 offset : rawOffset ,
204213 originAlign : align ,
205214 } ) ;
0 commit comments