我有一个 <List /> 的行项目 DOM 高度未知

I have a <List /> with row items of unknown DOM height

给定一个在每行中包含可变内容的反应虚拟化列表,DOM 高度需要通过 rowHeight 函数计算 - 但是因为在呈现行之前调用它我不确定如何实际获得行高。

给出的动态列表行高示例基本上偏离了列表项属性中的预定义数字,这无济于事。

我想我要做的是以默认高度呈现页面上的行,然后在 DOM 上获取溢出高度并将其设置为行高。我怎样才能连接到 afterRowRender 函数或类似的东西?我想性能会受到影响,所以也许有更好的方法来做到这一点,我想念。

查看 CellMeasurer. You can see it in action here 上的文档。

基本上你会寻找这样的东西:

import React from 'react';
import { CellMeasurer, CellMeasurerCache, Grid } from 'react-virtualized';

const cache = new CellMeasurerCache({
  fixedWidth: true,
  minHeight: 50,
});

function rowRenderer ({ index, isScrolling, key, parent, style }) {
  const source // This comes from your list data

  return (
    <CellMeasurer
      cache={cache}
      columnIndex={0}
      key={key}
      parent={parent}
      rowIndex={index}
    >
      {({ measure }) => (
        // 'style' attribute required to position cell (within parent List)
        <div style={style}>
          // Your content here
        </div>
      )}
    </CellMeasurer>
  );
}

function renderList (props) {
  return (
    <List
      {...props}
      deferredMeasurementCache={cache}
      rowHeight={cache.rowHeight}
      rowRenderer={rowRenderer}
    />
  );
}