1

我正在使用 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')
);
4

1 回答 1

0

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

于 2017-05-08T21:16:13.277 回答