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

Function useDynamicRowHeight

lib/components/list/useDynamicRowHeight.ts:23–198  ·  view source on GitHub ↗
({
  defaultRowHeight,
  initialRowHeights,
  key
}: {
  /**
   * Estimated height (in pixels) of rows that have not yet been measured.
   */
  defaultRowHeight: number;

  /**
   * Row heights (in pixels) that are known or can be estimated ahead of time, e.g. estimated from each row's content.
   * This can be either an array (where each item is the height of the row at that index) or a Map of row index to height.
   *
   * These values are used in place of `defaultRowHeight` until rows are rendered and measured,
   * so providing them can make server-rendered lists and initial scroll positions more accurate.
   *
   * ℹ️ This value is only read when the hook first mounts and when the `key` changes.
   */
  initialRowHeights?: ReadonlyMap<number, number> | readonly number[];

  /**
   * Changing this value clears all measured row heights,
   * e.g. when the list renders a different set of data.
   */
  key?: string | number;
})

Source from the content-addressed store, hash-verified

21}
22
23export function useDynamicRowHeight({
24 defaultRowHeight,
25 initialRowHeights,
26 key
27}: {
28 /**
29 * Estimated height (in pixels) of rows that have not yet been measured.
30 */
31 defaultRowHeight: number;
32
33 /**
34 * Row heights (in pixels) that are known or can be estimated ahead of time, e.g. estimated from each row's content.
35 * This can be either an array (where each item is the height of the row at that index) or a Map of row index to height.
36 *
37 * These values are used in place of `defaultRowHeight` until rows are rendered and measured,
38 * so providing them can make server-rendered lists and initial scroll positions more accurate.
39 *
40 * ℹ️ This value is only read when the hook first mounts and when the `key` changes.
41 */
42 initialRowHeights?: ReadonlyMap<number, number> | readonly number[];
43
44 /**
45 * Changing this value clears all measured row heights,
46 * e.g. when the list renders a different set of data.
47 */
48 key?: string | number;
49}) {
50 const [state, setState] = useState<{
51 key: string | number | undefined;
52 map: Map<number, number>;
53 }>(() => ({
54 key,
55 map: createInitialMap(initialRowHeights)
56 }));
57
58 if (state.key !== key) {
59 setState({
60 key,
61 map: createInitialMap(initialRowHeights)
62 });
63 }
64
65 const { map } = state;
66
67 const getAverageRowHeight = useCallback(() => {
68 let totalHeight = 0;
69
70 map.forEach((height) => {
71 totalHeight += height;
72 });
73
74 if (totalHeight === 0) {
75 return defaultRowHeight;
76 }
77
78 return totalHeight / map.size;
79 }, [defaultRowHeight, map]);
80

Callers 15

ExampleFunction · 0.90
DynamicListFunction · 0.90
DynamicListFunction · 0.90
DynamicListFunction · 0.90
AriaHiddenExampleFunction · 0.90
PendingScrollExampleFunction · 0.90
SmoothScrollExampleFunction · 0.90
ExampleFunction · 0.90
DynamicExampleFunction · 0.90
ItemWithoutIndexFunction · 0.90
HorizontalExampleFunction · 0.90

Calls 8

useStableCallbackFunction · 0.90
getItemIndexFunction · 0.90
getOwnerWindowFunction · 0.90
getResizeObserverClassFunction · 0.90
createInitialMapFunction · 0.85
disconnectMethod · 0.80
observeMethod · 0.80
unobserveMethod · 0.80

Tested by 11

ExampleFunction · 0.72
DynamicListFunction · 0.72
DynamicListFunction · 0.72
DynamicListFunction · 0.72
AriaHiddenExampleFunction · 0.72
PendingScrollExampleFunction · 0.72
SmoothScrollExampleFunction · 0.72
ExampleFunction · 0.72
DynamicExampleFunction · 0.72
ItemWithoutIndexFunction · 0.72
HorizontalExampleFunction · 0.72