【问题标题】:Dynamic height with infinite scroll (react-virtualized)无限滚动的动态高度(反应虚拟化)
【发布时间】:2018-01-03 06:43:51
【问题描述】:

我正在使用“react-virtualized”来呈现无限滚动列表。

但是,我无法动态呈现 rowHeight。试过了,不过只对桌面有影响,感觉不对。

我尝试按照示例进行操作,但没有成功。

计算真实行高的正确方法是什么?

它应该响应移动设备。

这是一个例子:

https://codesandbox.io/s/ADARgvlxB

class App extends React.Component {

  render() {

    return (
      <div>

        <AutoSizer>
        {({ height, width }) => (
        <List
          height={600}
          width={width}
          rowCount={foo.length}
          rowHeight={({ index }) => {
            const x = foo[index];
            if (x.name.length < 10) { return 20; } 
            else if (x.name.length > 9) { return 40;}
          }}
          rowRenderer={({ index, style }) => {
            const x = foo[index];
            return (
              <div key={index} style={style}>
                {x.name}
              </div>
            );
          }}
        />
        )}
        </AutoSizer>
      </div>
    );
  }
}

【问题讨论】:

    标签: javascript reactjs react-virtualized


    【解决方案1】:

    计算真实行高的正确方法是什么?

    这就是 react-virtualized CellMeasurer component 的用途。你可以看到demo of it measuring dynamic heights here。演示的源代码是*.example.js files here

    【讨论】:

    • 正如我所提到的,我尝试按照他们的示例进行操作,但没有成功。图片已设置高度(px),而我将考虑文字。
    • 您链接到的代码沙箱似乎没有遵循示例,因为它不使用CellMeasurer。再次查看我在上面提供的链接。
    猜你喜欢
    • 2018-01-18
    • 2019-06-12
    • 2018-04-28
    • 2017-08-20
    • 1970-01-01
    • 2017-10-05
    • 2020-11-14
    • 2017-01-25
    • 2020-08-18
    相关资源
    最近更新 更多