Skip to Content
TelamonJS

useTableState

Read the state of a DataTable or ListPage from the shared table store

Every DataTable and ListPage keeps its state (search, filters, sorting, pagination, selection, view mode) in one shared Zustand store, keyed by a table id. useTableState(tableId) reads the whole state of one table, so any component on the page — a header counter, an export button, a bulk-action bar — can follow what the table shows.

'use client'

import { useTableState } from '@telamonjs/ui'

export function UsersSelectionCount() {
  const { rowSelection, globalFilter } = useTableState('users')
  const selected = Object.values(rowSelection).filter(Boolean).length

  return (
    <span>
      {selected} selected{globalFilter ? ` matching "${globalFilter}"` : ''}
    </span>
  )
}

The table id is the tableId prop of ListPage (it defaults to config.module.route):

<ListPage config={usersConfig} tableId='users' />

State lives in memory

The store survives client-side navigation but not a full reload. To keep pagination in the URL, set urlSync on ListPage; for other state, use useUrlSync.


API

useTableState(tableId: string): TableState

Returns the table's state, or the defaults below when the table was never initialised. The selector is shallow-compared, so the component re-renders only when a field actually changes.

FieldTypeDefault
globalFilterstring''
columnFiltersRecord<string, unknown>{}
sortingSortField[] ({ field, order: 'asc' | 'desc' })[]
pagenumber1
pageSizenumber10
rowSelectionRecord<string, boolean>{}
columnVisibilityRecord<string, boolean>{}
viewMode'compact' | 'comfortable' | 'spacious''comfortable'
currentViewMode'table' | 'cards''table'
isFullscreenbooleanfalse
showFiltersbooleanfalse

Reading a single field

useTableState only reads. When a component needs only the sorting, useTableSorting(tableId) (SortField[]) subscribes to that slice and re-renders less.


Pitfalls

  • Two lists, one table id. Two ListPages for the same config on one screen share their state, because both default to config.module.route. Give each a distinct tableId.
  • A custom list doesn't need a second pagination state. DataTable already paginates from this store when given a tableId.

On this page