我正在尝试使用 react-window-infinite-loader 进行无限滚动,并且每次加载项目时,滚动都会回到顶部。因此,如果我在列表中的 100 项并且加载了更多数据,我会回到顶部并必须重新开始。这是我正在尝试的代码
const isItemLoaded = itemsCount => index => {
return (itemsCount -index) > 5;
}
//arbitrary high number
const itemCount = 10000000;
function loader(props) => {
const [data, setData] = useState([]);
const loadMoreData= async () => {
const payload = await callApi(//with proper params)
setData(data => [...data,...payload.data]
}
return(
<AutoSizer >
{({ height, width }) => (
<InfiniteLoader
isItemLoaded={isItemLoaded(leadCount)}
itemCount={itemCount}
loadMoreItems={loadMoreItems}
>
{({ onItemsRendered, ref }) => (
<List
key={`${width}${height}${data.length}`}
height={height}
width={width}
itemCount={data.length}
itemSize={162}
onItemsRendered={onItemsRendered}
ref={ref}
>
{({ index, style }) => (
<div style={style}>
<DataCard
key={'visitor_' + index}
{...data[index]}
/>
</div>
)}
</List>
)}
</InfiniteLoader>
)}
</AutoSizer>
)
}