Lazy loading/Infinite scrolling
Load data based on the visible viewport of the grid
CanvasGrid invokes onViewPortChange callback whenever user scrolls or pans around the spreadsheet.
This can be used to load data for the next set of visible rows.
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
@rowsncolumns/spreadsheet-state exports the useAsyncDatasource hook to load paged data for the current viewport while caching nearby pages.
Usage
Notes:
bufferPageskeeps a cushion of pages around the viewport.maxCachedPagescaps total cached pages and evicts the farthest ones.rowCountprevents over-fetching beyond your backend total.
Last updated