useVirtualListDimensionChange.tsx 631 B

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