javascript - React-virtualized 动态高度列表呈现最初堆叠的所有内容

标签 javascript reactjs react-native react-virtualized

我正在尝试使用 react-virtualized 来虚拟化一个列表,其中某些行具有不同的高度,而且该列表占用了父级中的所有空间。我正在尝试使用 CellMeasurer、AutoSizer 和 List 组件来完成此任务。

我的包版本如下:

react: "16.8.6"
react-dom: "16.8.6"
react-virtualized: "^9.21.1"
import React, { PureComponent } from 'react';
import 'react-virtualized/styles.css';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import { CellMeasurer, CellMeasurerCache, List } from 'react-virtualized';


class Table extends PureComponent {

  rowRenderer = ({ index, style, key }) => {
    return (
      <CellMeasurer
        cache={this.cache}
        columnIndex={0}
        key={key}
        parent={parent}
        rowIndex={index}
      >
        <div style={style} key={key}>
          content
        </div>
      </CellMeasurer>
    );
  }

  cache = new CellMeasurerCache({
    defaultHeight: 24,
    fixedWidth: true,
  });

  renderAutoSizerContent = () => {
    return this.RenderList;
  }

  RenderList = ({ height, width }) => {
    return (<List
      items={this.props.items}
      width={width}
      height={height}
      rowCount={this.props.items.length}
      rowHeight={this.cache.rowHeight}
      rowRenderer={this.rowRenderer}
      deferredMeasurementCache={this.cache}
    />
    );
  }


  render() {
    return (
      <AutoSizer
        items={ this.props.items}
      >
        {this.renderAutoSizerContent()}
      </AutoSizer>
    );
  }
}

export default Table;

在初始渲染后,一切看起来像这样。由于某种原因,数组中每个元素的 top 属性都是 0:

enter image description here

在滚动或触发重新渲染后,项目似乎获得了它们的 top 属性和以下渲染。在实际代码中,我的一些元素具有高度差异,但高度似乎默认为我为 CellMeasurerCache 构造函数提供的 defaultHeight。

After scrolling once the list seems to render, but still some of the heights are still wrong

如何使初始渲染器具有每个元素的 top 属性,以及如何正确计算高度?我在此处显示的代码中做错了什么?

最佳答案

在您的 rowRenderer 组件中,您为 CellMeasurer 提供了 parent 属性,但 parent 未定义。
如文档中所写,您可以从 rowRenderer 获取父级:https://github.com/bvaughn/react-virtualized/blob/master/docs/List.md#rowrenderer


所以你的 rowRenderer 组件应该是:

  rowRenderer = ({ index, style, key, parent }) => {
      return (
        <CellMeasurer
            cache={this.cache}
            columnIndex={0}
            key={key}
            parent={parent}
            rowIndex={index}
        >
           <div style={style} key={key}>
              {items[index]}
          </div>
        </CellMeasurer>
       );
  }

您还可以使用工作示例检查此代码沙箱:https://codesandbox.io/s/material-demo-yw1k8?fontsize=14

关于javascript - React-virtualized 动态高度列表呈现最初堆叠的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57522189/

相关文章:

javascript - ReactJS - 使用 map 循环遍历数据数组 Prop 时遇到问题

image - 在 React Native 中为图像添加填充?

reactjs - 当它在设备上运行时,你如何调试 react-native?

javascript - 世博客户端 : Fast Refresh not working (React Native)

javascript - Chrome扩展禁用js触发器

javascript - 当 option.amount 等于零时获取数组中对象的名称

带有时区的javascript日期格式

javascript - 文本框中的最大值和最小值

javascript - 如何在 redux 工具包中将参数传递给 createAsyncThunk?

Reactjs, typescript 无法在组件内设置初始状态