useVirtualListDimentionChange.tsx 599 B

123456789101112131415161718192021222324
  1. import {RefObject, useCallback} from 'react';
  2. import {CellMeasurerCache, List} from 'react-virtualized';
  3. type Opts = {
  4. cache: CellMeasurerCache;
  5. listRef: RefObject<List>;
  6. };
  7. export type OnDimensionChange = (index: number) => void;
  8. export default function useVirtualListDimentionChange({cache, listRef}: Opts) {
  9. const handleDimensionChange = useCallback(
  10. (index: number) => {
  11. cache.clear(index, 0);
  12. listRef.current?.recomputeGridSize({rowIndex: index});
  13. listRef.current?.forceUpdateGrid();
  14. },
  15. [cache, listRef]
  16. );
  17. return {
  18. handleDimensionChange,
  19. };
  20. }