我在 itemCount 中给出了一个任意数字,因为我的列表长度未知。当我向下滚动时,infiniteloader 仅第一次正常加载数据。但是在我的窗口视图完全空白后它会加载。
假设我的数组列表在每次提取时包含 10 个项目。如何在向下滚动并及时显示内容时立即加载数据?
const CARD_SIZE = 265;
class CardList extends PureComponent {
getItemData = memoize((itemsPerRow, newItems) => ({
itemsPerRow,
newItems
}))
Row = ({data, index, style }) => {
const { itemsPerRow, newItems } = data;
const items = [];
const fromIndex = index * itemsPerRow;
const toIndex = Math.min(fromIndex + itemsPerRow, newItems.length);
for (let i = fromIndex; i < toIndex; i++) {
items.push(
<Card
key={newItems[i]}
style={style}
token={this.props.token}
disableButton={this.props.disableButton}
image={newItems[i]} />
);
}
return (
<div style={style}>
{items}
</div>
);
}
render() {
const { newItems, loadMore } = this.props;
return (
<div style={{marginTop: "10px", height: "100vh" }}>
<AutoSizer>
{
({ height, width }) => {
const itemsPerRow = Math.floor(width / CARD_SIZE) || 1;
// const rowCount = Math.ceil(newItems.length / itemsPerRow);
const itemData = this.getItemData(itemsPerRow, newItems);
return (
<InfiniteLoader
isItemLoaded={index => index < newItems.length - 1}
itemCount={5000}
loadMoreItems={loadMore}
>
{({ onItemsRendered, ref }) => (
<List
height={height}
itemCount={5000}
itemData={itemData}
itemSize={CARD_SIZE}
width={width}
overscanCount={1}
onItemsRendered={onItemsRendered}
ref={ref}
>
{ this.Row }
</List>
)}
</InfiniteLoader>
)
}
}
</AutoSizer>
</div>
);
}
}