简体   繁体   English

React 虚拟化动态高度列表呈现最初堆叠的所有内容

[英]React-virtualized dynamic height list renders everything stacked initially

I am trying to use react-virtualized to virtualize a list where some rows have varying heights, and also the list takes up all the space in the parent.我正在尝试使用 react-virtualized 来虚拟化一个列表,其中某些行具有不同的高度,并且该列表占用了父项中的所有空间。 I am trying to accomplish this with the CellMeasurer, AutoSizer and List components.我正在尝试使用 CellMeasurer、AutoSizer 和 List 组件来实现这一点。

My package versions are as follows:我的包版本如下:

react-virtualized: "^9.21.1"反应虚拟化:“^9.21.1”

react: "16.8.6"反应:“16.8.6”

react-dom: "16.8.6"反应域:“16.8.6”

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;

After the initial render everything looks like this.初始渲染后,一切看起来像这样。 For some reason the top attribute is 0 for every element in the array:出于某种原因,数组中每个元素的 top 属性都是 0:

在此处输入图片说明

After scrolling or triggering a rerender the items seem to get their top property and the following renders.滚动或触发重新渲染后,项目似乎获得了它们的 top 属性和以下渲染。 In the actual code some of my elements have height variance, but the height seems to default to the defaultHeight I give to the CellMeasurerCache constructor anyway.在实际代码中,我的一些元素具有高度差异,但高度似乎默认为我给 CellMeasurerCache 构造函数的 defaultHeight。

滚动列表似乎呈现后,但仍有一些高度仍然错误

How do I get the initial render to have top property for each element, and how do I get the heights to calculate correctly?如何使初始渲染具有每个元素的 top 属性,以及如何正确计算高度? What am I doing wrong in the code I have shown here?我在这里显示的代码中做错了什么?

In your rowRenderer component, you provided parent prop to CellMeasurer but parent is undefined.rowRenderer组件中,您为CellMeasurer提供了parent道具,但 parent 未定义。
You get parent from rowRenderer as written in the docs: https://github.com/bvaughn/react-virtualized/blob/master/docs/List.md#rowrenderer您从 rowRenderer 获取父项,如文档中所述: https : //github.com/bvaughn/react-virtualized/blob/master/docs/List.md#rowrenderer


So your rowRenderer component should be:所以你的 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>
       );
  }

You can also check this code sandbox with working example: https://codesandbox.io/s/material-demo-yw1k8?fontsize=14您还可以使用工作示例检查此代码沙箱: https : //codesandbox.io/s/material-demo-yw1k8?fontsize=14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM