useVirtualizedList.tsx 616 B

1234567891011121314151617181920212223
  1. import {DependencyList, RefObject, useEffect, useMemo} from 'react';
  2. import {CellMeasurerCache, CellMeasurerCacheParams, List} from 'react-virtualized';
  3. type Opts = {
  4. cellMeasurer: CellMeasurerCacheParams;
  5. deps: DependencyList;
  6. ref: RefObject<List>;
  7. };
  8. function useVirtualizedList({cellMeasurer, deps, ref}: Opts) {
  9. const cache = useMemo(() => new CellMeasurerCache(cellMeasurer), [cellMeasurer]);
  10. // Restart cache when items changes
  11. useEffect(() => {
  12. cache.clearAll();
  13. ref.current?.forceUpdateGrid();
  14. }, [cache, ref, deps]);
  15. return {
  16. cache,
  17. };
  18. }
  19. export default useVirtualizedList;