MCPcopy Create free account
hub / github.com/bvaughn/react-window / useVirtualizer

Function useVirtualizer

lib/core/useVirtualizer.ts:30–580  ·  view source on GitHub ↗
({
  containerElement,
  containerStyle,
  defaultContainerSize = 0,
  defaultScrollOffset = 0,
  direction,
  isRtl = false,
  itemCount,
  itemProps,
  itemSize: itemSizeProp,
  onResize,
  overscanCount,
  scrollElement
}: {
  containerElement: HTMLElement | null;
  containerStyle?: CSSProperties;
  defaultContainerSize?: number;
  defaultScrollOffset?: 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;
  scrollElement?: ScrollElement | undefined;
})

Source from the content-addressed store, hash-verified

28import { useItemSize } from "./useItemSize";
29
30export function useVirtualizer<Props extends object>({
31 containerElement,
32 containerStyle,
33 defaultContainerSize = 0,
34 defaultScrollOffset = 0,
35 direction,
36 isRtl = false,
37 itemCount,
38 itemProps,
39 itemSize: itemSizeProp,
40 onResize,
41 overscanCount,
42 scrollElement
43}: {
44 containerElement: HTMLElement | null;
45 containerStyle?: CSSProperties;
46 defaultContainerSize?: number;
47 defaultScrollOffset?: number;
48 direction: Direction;
49 isRtl?: boolean;
50 itemCount: number;
51 itemProps: Props;
52 itemSize: number | string | SizeFunction<Props>;
53 onResize:
54 | ((
55 size: { height: number; width: number },
56 prevSize: { height: number; width: number }
57 ) => void)
58 | undefined;
59 overscanCount: number;
60 scrollElement?: ScrollElement | undefined;
61}) {
62 // Lists that scroll with an outer element (e.g. the page) render at their full size;
63 // the visible region is measured relative to the outer element's viewport.
64 const isScrollElementMode = scrollElement !== undefined;
65 const scrollTarget = isScrollElementMode
66 ? getScrollTarget(containerElement, scrollElement)
67 : containerElement;
68
69 const { height = defaultContainerSize, width = defaultContainerSize } =
70 useResizeObserver({
71 defaultHeight:
72 direction === "vertical" ? defaultContainerSize : undefined,
73 defaultWidth:
74 direction === "horizontal" ? defaultContainerSize : undefined,
75 element: containerElement,
76 mode: direction === "vertical" ? "only-height" : "only-width",
77 style: containerStyle
78 });
79
80 const prevSizeRef = useRef<{ height: number; width: number }>({
81 height: 0,
82 width: 0
83 });
84
85 // Percentage item sizes are relative to the scroll element's viewport in scroll element mode
86 const [viewportSize, setViewportSize] = useState(defaultContainerSize);
87

Callers 6

ExampleFunction · 0.50
ExampleFunction · 0.50
ExampleFunction · 0.50
HeadlessTableRouteFunction · 0.50

Calls 15

getScrollTargetFunction · 0.90
useResizeObserverFunction · 0.90
useItemSizeFunction · 0.90
useCachedBoundsFunction · 0.90
useIsHydratingFunction · 0.90
adjustScrollOffsetForRtlFunction · 0.90
getStartStopIndicesFunction · 0.90
shallowCompareFunction · 0.90
useStableCallbackFunction · 0.90
getOwnerWindowFunction · 0.90
getResizeObserverClassFunction · 0.90

Tested by

no test coverage detected