Save and restore selected columns
Persisting user preferences
TanStack · Type Alias
VisibilityState is TanStack Table’s map of column IDs to visibility flags. Use it to represent which columns are shown or hidden and to control column-visibility state.
VisibilityState models TanStack Table's column-visibility state as a map from column identifiers to booleans. A true value marks a column visible, while false hides it; identifiers omitted from the map are treated as visible by the feature.
export type VisibilityState = Record<string, boolean>A type alias for Record<string, boolean>; it describes the table's column-visibility map.
import type { VisibilityState } from '@tanstack/react-table'
const visibility: VisibilityState = {
id: true,
email: false,
}
const visibleColumns = Object.entries(visibility)
.filter(([, isVisible]) => isVisible)
.map(([columnId]) => columnId)
console.log(visibleColumns)Creates a visibility map in which each column identifier is associated with whether that column should be shown.
Persisting user preferences
Building column controls
Controlled table state
VisibilityState is a Record<string, boolean>. The string key identifies a column, and its boolean value represents that column's visibility.
Follow-up: What happens when a column is absent from the record? It remains visible because missing column IDs are treated as visible.
Pass the state through the table's columnVisibility state property. To control updates externally, provide onColumnVisibilityChange, which receives an updater for VisibilityState.
Follow-up: Why must external state be persisted when using the change callback? The callback replaces the table's built-in state management.
setColumnVisibility accepts an Updater<VisibilityState>, so it can receive either a new visibility value or an updater function. The table also exposes toggling APIs for individual and all columns.
Follow-up: Which API is useful for a checkbox? getToggleVisibilityHandler returns a handler intended to bind to a checkbox input.
VisibilityTableStatecolumnVisibilityonColumnVisibilityChangesetColumnVisibilitytoggleVisibilitytoggleAllColumnsVisiblegetIsVisible