首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测量react-virtualized列表中的行高

如何测量react-virtualized列表中的行高
EN

Stack Overflow用户
提问于 2016-09-21 01:59:52
回答 1查看 6.4K关注 0票数 5

对于如何使用react-virtualized实现列表的动态高度,我有点不确定。

我有一个组件如下所示:

代码语言:javascript
复制
import { List } from 'react-virtualized';
<List
    height={400}
    rowCount={_.size(messages)}
    rowHeight={(index) => {
        return 100; // This needs to measure the dom.
    }}
    rowRenderer={({ key, index, style }) => <Message style={style} {...messages[index]} />}}
    width={300}
/>

我已经看过如何根据文档使用CellMeasurer,文档说它可以与List组件一起使用,但我不知道这个示例实际是如何工作的……

我也试图弄清楚它是如何在demo code中实现的,但也走到了死胡同。

谁能帮助我如何测量DOM,以获得每个项目的高度动态。

EN

回答 1

Stack Overflow用户

发布于 2016-09-21 03:30:52

很抱歉,您发现文档令人困惑。我会试着更新它们,让它们更清晰。希望这能有所帮助:

代码语言:javascript
复制
import { CellMeasurer, List } from 'react-virtualized';

function renderList (listProps) {
  return (
    <CellMeasurer
      cellRenderer={
        // CellMeasurer expects to work with a Grid
        // But your rowRenderer was written for a List
        // The only difference is the named parameter they
        // So map the Grid params (eg rowIndex) to List params (eg index)
        ({ rowIndex, ...rest }) => listProps.cellRenderer({ index: rowIndex, ...rest })
      }
      columnCount={1}
      rowCount={listProps.rowCount}
      width={listProps.width}
    >
      {({ getRowHeight, setRef }) => (
        <List
          {...listProps}
          ref={setRef}
          rowHeight={getRowHeight}
        />
      )}
    </CellMeasurer>
  )
}

还有这个组件here的演示,展示了它是如何使用的。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39600808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档