=> {
const response = await fetch(`/api/items?start=${startIndex}&end=${stopIndex}`);
const data: Item[] = await response.json();
setItems(prev => {
const newMap = new Map(prev);
data.forEach((item, i) => {
newMap.set(startIndex + i, item);
});
return newMap;
});
},
[]
);
const Row = ({ index, style }: { index: number; style: React.CSSProperties }) => {
const item = items.get(index);
return (
{item ? (
<>
{item.name}
{item.status}
>
) : (
Loading...
)}
);
};
return (
{({ onRowsRendered }) => (
{
onRowsRendered({ startIndex: visibleStartIndex, stopIndex: visibleStopIndex });
}}
>
{Row}
)}
);
}
```
--------------------------------
### Using onRowsRendered with FixedSizeList and VariableSizeList
Source: https://context7.com/bvaughn/react-window-infinite-loader/llms.txt
This snippet demonstrates how to use the `onRowsRendered` callback provided by the `useInfiniteLoader` hook with both `FixedSizeList` and `VariableSizeList` from 'react-window'. The callback should be invoked with the currently visible row indices to trigger data loading. It can use either `visibleStartIndex` and `visibleStopIndex` for precise loading or `overscanStartIndex` and `overscanStopIndex` for eager loading.
```tsx
import { FixedSizeList, VariableSizeList } from "react-window";
import { useInfiniteLoader } from "react-window-infinite-loader";
function ListWithCallback() {
const onRowsRendered = useInfiniteLoader({
isRowLoaded,
loadMoreRows,
rowCount: 1000,
});
return (
<>
{/* FixedSizeList usage */}
{
// Use visible indices for more precise loading
onRowsRendered({
startIndex: visibleStartIndex,
stopIndex: visibleStopIndex,
});
// Or use overscan indices for eager loading
// onRowsRendered({
// startIndex: overscanStartIndex,
// stopIndex: overscanStopIndex,
// });
}}
>
{Row}
{/* VariableSizeList usage - same callback pattern */}
(index % 2 === 0 ? 50 : 35)}
onItemsRendered={({ visibleStartIndex, visibleStopIndex }) => {
onRowsRendered({
startIndex: visibleStartIndex,
stopIndex: visibleStopIndex,
});
}}
>
{Row}
>
);
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.