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

Function useDynamicRowHeight

lib/components/list/useDynamicRowHeight.ts:7–139  ·  view source on GitHub ↗
({
  defaultRowHeight,
  key
}: {
  defaultRowHeight: number;
  key?: string | number;
})

Source from the content-addressed store, hash-verified

5import type { DynamicRowHeight } from "./types";
6
7export function useDynamicRowHeight({
8 defaultRowHeight,
9 key
10}: {
11 defaultRowHeight: number;
12 key?: string | number;
13}) {
14 const [state, setState] = useState<{
15 key: string | number | undefined;
16 map: Map<number, number>;
17 }>({
18 key,
19 map: new Map()
20 });
21
22 if (state.key !== key) {
23 setState({
24 key,
25 map: new Map()
26 });
27 }
28
29 const { map } = state;
30
31 const getAverageRowHeight = useCallback(() => {
32 let totalHeight = 0;
33
34 map.forEach((height) => {
35 totalHeight += height;
36 });
37
38 if (totalHeight === 0) {
39 return defaultRowHeight;
40 }
41
42 return totalHeight / map.size;
43 }, [defaultRowHeight, map]);
44
45 const getRowHeight = useCallback(
46 (index: number) => {
47 const measuredHeight = map.get(index);
48 if (measuredHeight !== undefined) {
49 return measuredHeight;
50 }
51
52 // Keep estimates stable when rowProps change (see issue #863), but do not
53 // store them as measurements or include them in the measured average.
54 return defaultRowHeight;
55 },
56 [defaultRowHeight, map]
57 );
58
59 const setRowHeight = useCallback((index: number, size: number) => {
60 setState((prevState) => {
61 if (prevState.map.get(index) === size) {
62 return prevState;
63 }
64

Callers 10

ExampleFunction · 0.90
PendingScrollExampleFunction · 0.90
ExampleFunction · 0.90
ScratchpadRouteFunction · 0.85
ExampleFunction · 0.85
ExampleFunction · 0.85
PageFunction · 0.85
ListFunction · 0.85
ListDynamicRouteFunction · 0.85

Calls 5

useStableCallbackFunction · 0.90
assertFunction · 0.90
disconnectMethod · 0.80
observeMethod · 0.80
unobserveMethod · 0.80

Tested by 3

ExampleFunction · 0.72
PendingScrollExampleFunction · 0.72
ExampleFunction · 0.72