【问题标题】:React-Virtualizer InfiniteLoader startIndex and stopIndex are the same valueReact-Virtualizer InfiniteLoader startIndex 和 stopIndex 是相同的值
【发布时间】:2017-10-06 21:26:28
【问题描述】:

我正在使用 React-Virtualizer,看起来加载行的初始调用的 startIndex 为 0,stopIndex 为 0。如果数组已经有一些项目,那么 startIndex 是数组长度,而stopIndex 是相同的数组长度。我不确定为什么会这样,但显然这是个问题。

您可以在此处查看可重现的示例:

https://plnkr.co/edit/TP5BTGNA0Me1Rz7Q7Ge9?p=preview

这是 JSX 文件:

var List = ReactVirtualized.List;
var InfiniteLoader = ReactVirtualized.InfiniteLoader;
var AutoSizer = ReactVirtualized.AutoSizer;
var CellMeasurer = ReactVirtualized.CellMeasurer;
var CellMeasurerCache = ReactVirtualized.CellMeasurerCache;

// Define a component:
var Main = React.createClass({
  getInitialState: function() {
    return {
      hasNextPage: true,
      nextPageLoading: false,
      totalCount: 100,
    }
  },
  loadNextPage: function({startIndex, stopIndex}) {
    console.log(startIndex, stopIndex) // THIS ISN'T RIGHT?
  },
  render: function() {
      const rows = []
      const rowsCount = this.state.hasNextPage ? rows.length + 1 : rows.length

      // Only load 1 page of items at a time.
      // Pass an empty callback to InfiniteLoader in case it asks us to load more than once.
      const loadMoreRows = this.state.nextPageLoading ? () => {} : this.loadNextPage

      // Every row is loaded except for our loading indicator row.
      const isRowLoaded = ({ index }) => !this.state.hasNextPage || index < rows.length

      // Render a list item or a loading indicator.
      const rowRenderer = ({ index, key, style }) => {
        if (!isRowLoaded({ index })) {
          console.log("LOADING")
          return(
            <div style={style}>
              Loading...
            </div>
          )
        } else {
          console.log(rows[index])
          return(
            <div style={style}>
              {rows[index]}
            </div>
          )
        }
      }

      console.log(rows) // SHOWS THE ARRAY
      return(
        <InfiniteLoader
          isRowLoaded={isRowLoaded}
          loadMoreRows={loadMoreRows}
          rowCount={rowsCount}>
          {({ onRowsRendered, registerChild }) => (
            <div style={{height: 300}}>
              <AutoSizer>
                {({ height, width }) => (
                  <List
                    height={height}
                    width={width}
                    ref={registerChild}
                    onRowsRendered={onRowsRendered}
                    rowCount={this.state.totalCount}
                    rowHeight={46}
                    rowRenderer={rowRenderer}
                  />
                )}
              </AutoSizer>
            </div>
          )}
        </InfiniteLoader>
      );
  }
});


// Render your list
ReactDOM.render(
    <Main />, // What to render (an instance of the Main component)
  document.getElementById('example')
);

【问题讨论】:

    标签: reactjs react-virtualized


    【解决方案1】:

    这最终是因为 rowCount 设置为当前长度,而不是加载所有数据后的总长度。将其设置为服务器的总列表大小修复了它。

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 2020-09-17
      • 2018-02-05
      • 2018-10-04
      • 2017-10-06
      • 2018-07-13
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      相关资源
      最近更新 更多