Repository navigation
|
I have implemented performant column sizing as shown in this example in a table which also uses column visibility. However, because the table no longer updates on How can I get the performance benefits while also having columns un-hide at the correct size? |
Replies: 1 comment 1 reply
|
The cause is that the example only writes CSS variables for The fix is to re-run the writer when visibility changes as well as when sizing changes. v9 (current example, atoms): React.useLayoutEffect(() => {
const writeColumnSizeVars = () => {
const tableEl = tableRef.current
if (!tableEl) return
for (const header of table.getFlatHeaders()) {
tableEl.style.setProperty(`--header-${header.id}-size`, String(header.getSize()))
tableEl.style.setProperty(`--col-${header.column.id}-size`, String(header.column.getSize()))
}
tableEl.style.width = `${table.getTotalSize()}px`
}
writeColumnSizeVars()
const subs = [
table.atoms.columnSizing.subscribe(writeColumnSizeVars),
table.atoms.columnVisibility.subscribe(writeColumnSizeVars), // <- add this
]
return () => subs.forEach((s) => s.unsubscribe())
}, [])This doesn't cost you anything during a resize. Visibility doesn't change while you're dragging, so the extra subscription only fires when a column is actually shown or hidden. It also keeps I tested this with
v8 ( const columnSizeVars = React.useMemo(() => {
// ...same loop over table.getFlatHeaders()
}, [
table.getState().columnSizingInfo,
table.getState().columnSizing,
table.getState().columnVisibility, // <- add this
])You could also loop over |
The cause is that the example only writes CSS variables for
table.getFlatHeaders(), and that list only includes visible columns. A column that starts hidden never gets a--col-<id>-size/--header-<id>-sizevariable. Showing it doesn't changecolumnSizing, so the writer never runs again. The new cells then render withcalc(var(--col-b-size) * 1px), which is invalid because the variable doesn't exist, so the width falls back to auto.The fix is to re-run the writer when visibility changes as well as when sizing changes.
v9 (current example, atoms):