Skip to content

Commit cd80c52

Browse files
authoredJan 25, 2024
docs: clean up col resize and virtualization examples (#5298)
1 parent e6e02a9 commit cd80c52

File tree

5 files changed

+14
-63
lines changed

5 files changed

+14
-63
lines changed
 

‎examples/react/column-resizing-performant/src/main.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const defaultColumns: ColumnDef<Person>[] = [
6969
]
7070

7171
function App() {
72-
const [data, setData] = React.useState(() => makeData(200))
72+
const [data, _setData] = React.useState(() => makeData(200))
7373
const [columns] = React.useState<typeof defaultColumns>(() => [
7474
...defaultColumns,
7575
])
@@ -79,6 +79,10 @@ function App() {
7979
const table = useReactTable({
8080
data,
8181
columns,
82+
defaultColumn: {
83+
minSize: 60,
84+
maxSize: 800,
85+
},
8286
columnResizeMode: 'onChange',
8387
getCoreRowModel: getCoreRowModel(),
8488
debugTable: true,
@@ -125,11 +129,10 @@ function App() {
125129
<button onClick={() => rerender()} className="border p-2">
126130
Rerender
127131
</button>
128-
<pre style={{ minHeight: '30rem' }}>
132+
<pre style={{ minHeight: '10rem' }}>
129133
{JSON.stringify(
130134
{
131135
columnSizing: table.getState().columnSizing,
132-
columnSizingInfo: table.getState().columnSizingInfo,
133136
},
134137
null,
135138
2

‎examples/react/virtualized-columns/src/main.tsx

+6-17
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import { useVirtualizer } from '@tanstack/react-virtual'
1616

1717
import { makeColumns, makeData, Person } from './makeData'
1818

19-
//This is a dynamic row height example, which is more complicated, but allows for a more realistic table.
20-
//See https://tanstack.com/virtual/v3/docs/examples/react/table for a simpler fixed row height example.
2119
function App() {
2220
const columns = React.useMemo<ColumnDef<Person>[]>(
2321
() => makeColumns(1_000),
@@ -36,27 +34,18 @@ function App() {
3634

3735
const { rows } = table.getRowModel()
3836

37+
const visibleColumns = table.getVisibleLeafColumns()
38+
3939
//The virtualizers need to know the scrollable container element
4040
const tableContainerRef = React.useRef<HTMLDivElement>(null)
4141

42-
//get first 16 column widths and average them for column size estimation
43-
const averageColumnWidth = React.useMemo(() => {
44-
const columnsWidths =
45-
table
46-
.getRowModel()
47-
.rows[0]?.getCenterVisibleCells()
48-
?.slice(0, 16)
49-
?.map(cell => cell.column.getSize()) ?? []
50-
return columnsWidths.reduce((a, b) => a + b, 0) / columnsWidths.length
51-
}, [table.getRowModel().rows])
52-
53-
//we are using a slightly different virtualization strategy for columns in order to support dynamic row heights
42+
//we are using a slightly different virtualization strategy for columns (compared to virtual rows) in order to support dynamic row heights
5443
const columnVirtualizer = useVirtualizer({
55-
count: table.getVisibleLeafColumns().length,
56-
estimateSize: () => averageColumnWidth, //average column width in pixels
44+
count: visibleColumns.length,
45+
estimateSize: index => visibleColumns[index].getSize(), //estimate width of each column for accurate scrollbar dragging
5746
getScrollElement: () => tableContainerRef.current,
5847
horizontal: true,
59-
overscan: 3,
48+
overscan: 3, //how many columns to render on each side off screen each way (adjust this for performance)
6049
})
6150

6251
//dynamic row height virtualization - alternatively you could use a simpler fixed row height strategy without the need for `measureElement`

‎examples/react/virtualized-columns/src/makeData.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const makeColumns = num =>
55
return {
66
accessorKey: i.toString(),
77
header: 'Column ' + i.toString(),
8+
size: Math.floor(Math.random() * 150) + 100,
89
}
910
})
1011

‎examples/react/virtualized-infinite-scrolling/src/main.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function App() {
7979
)
8080

8181
//react-query has a useInfiniteQuery hook that is perfect for this use case
82-
const { data, fetchNextPage, isError, isFetching, isLoading } =
82+
const { data, fetchNextPage, isFetching, isLoading } =
8383
useInfiniteQuery<PersonApiResponse>({
8484
queryKey: [
8585
'people',

‎upgrade-examples.sh

-42
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.