11import React from 'react' ;
22
3- import { throttle } from 'lodash' ;
4-
5- import { calculateElementOffsetTop } from './utils' ;
3+ import { calculateElementOffsetTop , rafThrottle } from './utils' ;
64
75interface UseScrollBasedChunksProps {
86 parentRef : React . RefObject < HTMLElement > ;
@@ -14,7 +12,6 @@ interface UseScrollBasedChunksProps {
1412}
1513
1614const DEFAULT_OVERSCAN_COUNT = 1 ;
17- const THROTTLE_DELAY = 100 ;
1815
1916export const useScrollBasedChunks = ( {
2017 parentRef,
@@ -54,38 +51,45 @@ export const useScrollBasedChunks = ({
5451 return { start, end} ;
5552 } , [ parentRef , tableRef , rowHeight , chunkSize , overscanCount , chunksCount ] ) ;
5653
57- React . useEffect ( ( ) => {
54+ const updateVisibleChunks = React . useCallback ( ( ) => {
5855 const newRange = calculateVisibleRange ( ) ;
59-
6056 if ( newRange ) {
6157 setStartChunk ( newRange . start ) ;
6258 setEndChunk ( newRange . end ) ;
6359 }
64- } , [ chunksCount , calculateVisibleRange ] ) ;
60+ } , [ calculateVisibleRange ] ) ;
61+
62+ React . useEffect ( ( ) => {
63+ updateVisibleChunks ( ) ;
64+ } , [ chunksCount , updateVisibleChunks ] ) ;
6565
6666 const handleScroll = React . useCallback ( ( ) => {
67- const newRange = calculateVisibleRange ( ) ;
68- if ( newRange ) {
69- setStartChunk ( newRange . start ) ;
70- setEndChunk ( newRange . end ) ;
71- }
72- } , [ calculateVisibleRange ] ) ;
67+ updateVisibleChunks ( ) ;
68+ } , [ updateVisibleChunks ] ) ;
69+
70+ React . useEffect ( ( ) => {
71+ const throttledHandleZoom = rafThrottle ( ( ) => {
72+ updateVisibleChunks ( ) ;
73+ } ) ;
74+
75+ window . addEventListener ( 'resize' , throttledHandleZoom ) ;
76+
77+ return ( ) => {
78+ window . removeEventListener ( 'resize' , throttledHandleZoom ) ;
79+ } ;
80+ } , [ updateVisibleChunks ] ) ;
7381
7482 React . useEffect ( ( ) => {
7583 const container = parentRef ?. current ;
7684 if ( ! container ) {
7785 return undefined ;
7886 }
7987
80- const throttledHandleScroll = throttle ( handleScroll , THROTTLE_DELAY , {
81- leading : true ,
82- trailing : true ,
83- } ) ;
88+ const throttledHandleScroll = rafThrottle ( handleScroll ) ;
8489
8590 container . addEventListener ( 'scroll' , throttledHandleScroll ) ;
8691 return ( ) => {
8792 container . removeEventListener ( 'scroll' , throttledHandleScroll ) ;
88- throttledHandleScroll . cancel ( ) ;
8993 } ;
9094 } , [ handleScroll , parentRef ] ) ;
9195
0 commit comments