Lazy loading/Infinite scrolling
Load data based on the visible viewport of the grid
import { CanvasGrid } from "@rowsncolumns/spreadsheet"
const App = () => {
const [ sheetData, onChangeSheetData ] = useState<SheetData<T>>({})
<CanvasGrid
onViewPortChange={(viewport: ViewPortProps) => {
const {
columnStartIndex,
columnStopIndex,
rowStartIndex,
rowStopIndex,
visibleColumnStartIndex,
visibleColumnStopIndex,
visibleRowStartIndex,
visibleRowStopIndex
} = viewport
// Throttle fetch request
// Move it out of the render loop, this is just an example
throttle(
fetch(`/rows?start=${rowStartIndex}&end=${rowStopIndex}`)
.then(rowData => {
// Append new row data
onChangeSheetData(prev => {
const newRowData = prev[sheetId]
.splice(rowStartIndex, rowStopIndex - rowStartIndex, ...rowData)
return {
...prev,
[sheetId]: newRowData
}
})
})
, 300)
}}
/>
}Hooks for Lazy loading
Usage
Last updated