MCPcopy
hub / github.com/bvaughn/react-window / useVirtualizer

Function useVirtualizer

lib/core/useVirtualizer.ts:21–266  ·  view source on GitHub ↗
({
  containerElement,
  containerStyle,
  defaultContainerSize = 0,
  direction,
  isRtl = false,
  itemCount,
  itemProps,
  itemSize: itemSizeProp,
  onResize,
  overscanCount
}: {
  containerElement: HTMLElement | null;
  containerStyle?: CSSProperties;
  defaultContainerSize?: number;
  direction: Direction;
  isRtl?: boolean;
  itemCount: number;
  itemProps: Props;
  itemSize: number | string | SizeFunction<Props>;
  onResize:
    | ((
        size: { height: number; width: number },
        prevSize: { height: number; width: number }
      ) => void)
    | undefined;
  overscanCount: number;
})

Source from the content-addressed store, hash-verified

19import { useItemSize } from "./useItemSize";
20
21export function useVirtualizer<Props extends object>({
22 containerElement,
23 containerStyle,
24 defaultContainerSize = 0,
25 direction,
26 isRtl = false,
27 itemCount,
28 itemProps,
29 itemSize: itemSizeProp,
30 onResize,
31 overscanCount
32}: {
33 containerElement: HTMLElement | null;
34 containerStyle?: CSSProperties;
35 defaultContainerSize?: number;
36 direction: Direction;
37 isRtl?: boolean;
38 itemCount: number;
39 itemProps: Props;
40 itemSize: number | string | SizeFunction<Props>;
41 onResize:
42 | ((
43 size: { height: number; width: number },
44 prevSize: { height: number; width: number }
45 ) => void)
46 | undefined;
47 overscanCount: number;
48}) {
49 const { height = defaultContainerSize, width = defaultContainerSize } =
50 useResizeObserver({
51 defaultHeight:
52 direction === "vertical" ? defaultContainerSize : undefined,
53 defaultWidth:
54 direction === "horizontal" ? defaultContainerSize : undefined,
55 element: containerElement,
56 mode: direction === "vertical" ? "only-height" : "only-width",
57 style: containerStyle
58 });
59
60 const prevSizeRef = useRef<{ height: number; width: number }>({
61 height: 0,
62 width: 0
63 });
64
65 const containerSize = direction === "vertical" ? height : width;
66
67 const itemSize = useItemSize({ containerSize, itemSize: itemSizeProp });
68
69 useLayoutEffect(() => {
70 if (typeof onResize === "function") {
71 const prevSize = prevSizeRef.current;
72
73 if (prevSize.height !== height || prevSize.width !== width) {
74 onResize({ height, width }, { ...prevSize });
75
76 prevSize.height = height;
77 prevSize.width = width;
78 }

Callers 3

GridFunction · 0.90
ListFunction · 0.90

Calls 8

useResizeObserverFunction · 0.90
useItemSizeFunction · 0.90
useCachedBoundsFunction · 0.90
adjustScrollOffsetForRtlFunction · 0.90
getStartStopIndicesFunction · 0.90
useStableCallbackFunction · 0.90
getOffsetForIndexFunction · 0.90
shallowCompareFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…