@@ -257,8 +257,9 @@ function useVisibleRows(rows, rowHeight, safeAreaRef, expansion) {
257257 /**
258258 * decide which the start/end indexes should be, based on scroll position.
259259 * NOTE: this is called on scroll, so must not incur expensive checks/measurements - math only!
260+ * @param {number } rowCount - the number of rows in the dataset
260261 */
261- function setVisibleRowsForOffset ( ) {
262+ function setVisibleRowsForOffset ( rowCount ) {
262263 if ( ! safeAreaRef . current ) return console . warn ( 'cannot access ref' ) ;
263264 const scrollY = mainScrollerRef . current ?. scrollTop ?? 0 ;
264265 const offset = gridOffsetRef . current ;
@@ -270,7 +271,7 @@ function useVisibleRows(rows, rowHeight, safeAreaRef, expansion) {
270271 start = scrollY - offset ;
271272 }
272273 const startIndex = Math . floor ( start / rowHeight ) ;
273- const endIndex = Math . min ( Math . ceil ( end / rowHeight ) , rows . length ) ;
274+ const endIndex = Math . min ( Math . ceil ( end / rowHeight ) , rowCount ) ;
274275
275276 // don't set state if the offset didn't change
276277 setVisibleRange ( ( prev ) => {
@@ -293,12 +294,18 @@ function useVisibleRows(rows, rowHeight, safeAreaRef, expansion) {
293294 updateGlobals ( ) ;
294295
295296 // and set visible rows once the size is known
296- setVisibleRowsForOffset ( ) ;
297+ setVisibleRowsForOffset ( rows . length ) ;
297298
298299 const controller = new AbortController ( ) ;
299300
300301 // when the main area is scrolled, update the visible offset for the rows.
301- mainScrollerRef . current ?. addEventListener ( 'scroll' , setVisibleRowsForOffset , { signal : controller . signal } ) ;
302+ mainScrollerRef . current ?. addEventListener (
303+ 'scroll' ,
304+ ( ) => {
305+ setVisibleRowsForOffset ( rows . length ) ;
306+ } ,
307+ { signal : controller . signal } ,
308+ ) ;
302309
303310 return ( ) => {
304311 controller . abort ( ) ;
@@ -311,13 +318,13 @@ function useVisibleRows(rows, rowHeight, safeAreaRef, expansion) {
311318 if ( lastWindowHeight === window . innerHeight ) return ;
312319 lastWindowHeight = window . innerHeight ;
313320 updateGlobals ( ) ;
314- setVisibleRowsForOffset ( ) ;
321+ setVisibleRowsForOffset ( rows . length ) ;
315322 }
316323 window . addEventListener ( 'resize' , handler ) ;
317324 return ( ) => {
318325 return window . removeEventListener ( 'resize' , handler ) ;
319326 } ;
320- } , [ ] ) ;
327+ } , [ rows . length ] ) ;
321328
322329 useEffect ( ( ) => {
323330 if ( ! contentTubeRef . current ) return console . warn ( 'cannot find content tube' ) ;
@@ -331,7 +338,7 @@ function useVisibleRows(rows, rowHeight, safeAreaRef, expansion) {
331338 clearTimeout ( debounceTimer ) ;
332339 debounceTimer = setTimeout ( ( ) => {
333340 updateGlobals ( ) ;
334- setVisibleRowsForOffset ( ) ;
341+ setVisibleRowsForOffset ( rows . length ) ;
335342 } , 50 ) ;
336343 }
337344 } ) ;
@@ -341,7 +348,7 @@ function useVisibleRows(rows, rowHeight, safeAreaRef, expansion) {
341348 resizer . disconnect ( ) ;
342349 clearTimeout ( debounceTimer ) ;
343350 } ;
344- } , [ ] ) ;
351+ } , [ rows . length ] ) ;
345352
346353 return { start, end } ;
347354}
0 commit comments