Capítulo 19 de 20
The table example combines TanStack Table's row model with a vertical Virtualizer. The header remains rendered while only visible body rows are positioned.
const virtualizer = useVirtualizer({
count: rows.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 34,
overscan: 20,
})
<div ref={parentRef} className="container">
<div style={{ height: `${virtualizer.getTotalSize()}px` }}>
<table>
<thead>{/* table header groups */}</thead>
<tbody>
{virtualizer.getVirtualItems().map((virtualRow, index) => {
const row = rows[virtualRow.index]
return (
<tr
key={row.id}
style={{
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start - index * virtualRow.size}px)`,
}}
>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</td>
))}
</tr>
)
})}
</tbody>
</table>
</div>
</div>