DocsBlog
  • 1.4.0

  • light

    dark

    system

    Switch mode
  • Cerberus

    Acheron

    Elysium

Get Started
Components
Data Grid
Signals
Styling
Theming

Get Started

OverviewQuick StartColumnsContextTheme

Layout

DimensionsSizingSlotsOverlaysToolbarFooterPagination

Features

PinningSortingFiltering

Reference

API

On this page

Loading...

Loading...

Loading...

Loading...

Data Grid Context

A signal-driven store for reading and updating state in a highly performant way.

  • source
import { useDataGridContext } from '@cerberus/data-grid'

Overview

The Data Grid runs on a signal-driven store for reading and updating state in a highly performant way. This allows the grid to bypass React's internal reactivity system, preventing unnecessary re-renders and improving overall performance by manaing state outside of the scope of React components.

To learn more about signals and how they work, see the Signals documentation.

Using the context

To recieve the context from the Data Grid, you simply use the useDataGridContext hook like you would any other hook in React.

Reading Signals

When reading signals values from the context, you need to use them in a way that will ensure the reactivity is being tracked via the useRead hook from the Cerberus Signals library.

This will subscribe to the signal's reactivity and provide the latest value when used within a component.

Note

Accessing a context value without using the mentioned hook will not be reactive.

Using Actions

Since actions only write changes, you can use them directly from the store.

Store Reference

The Data Grid store provides both getter and setter helpers for accessing and updating state in a signal-based reactive manner:

Reactive Values

NameTypeDescription
columnsAccessor<InternalColumn<TData>[]>A list of processed columns.
orderedColumnsAccessor<InternalColumn<TData>[]>A list of ordered columns.
rowsAccessor<TData[]>A list of processed rows.
rowSizeAccessor<number>The height of the row.
rowCountAccessor<number>The current number of rows in the grid.
pageIndexAccessor<number>The current page index of the grid.
pageSizeAccessor<number>The current page size of the grid.
pageRangeAccessor<number[]>The page range options of the grid.
currentPageRangeAccessor<{ start: number; end: number }>The current page range.
pageCountAccessor<number>The current page count of the grid used for SSR pagination.
isServerPaginatedAccessor<boolean>If pagination is using server-based.
showColFilterAccessor<boolean>If the user has chosen the column filter feature option.
globalFilterAccessor<BaseFilterState>The current global filter value used for column filtering.
collFiltersAccessor<ColumnFilterState>The current column filters used.
filteredRowsAccessor<TData[]>The rows after filtering.
sortingAccessor<SortState[]>The current sorting state of the grid.
sortedRowsAccessor<TData[]>The rows after sorting.
phaseAccessor<Phase>The current phase of the grid layout.
pendingAccessor<boolean>The state of the pending prop of the Data Grid.
hasSkeletonAccessor<boolean>If a skeleton overlay option is provided.
rootCssVarsAccessor<Record<string, string>>The current CSS variables for the grid root element.
totalWidthAccessor<number>The total width of the grid (used for pinning).
visibleRowsAccessor<TData[]>The list of visible rows in the viewport.

Actions

NameTypeDescription
resizeColumn(columnId: string, delta: number) => voidResizes a column by updating its width.
setContainerWidthSetter<number>Updates the width of the grid container.
setPage(details: PageDetails) => voidUpdates the current page of the grid.
setPageIndexSetter<number>Updates the current page index of the grid.
setPageSize(size: number) => voidUpdates the page size of the grid.
setGlobalFilterSetter<BaseFilterState>Updates the global filter value.
setColFilterSetter<ColumnFilterState>Updates the col filter value.
setShowColFilterSetter<boolean>Updates the showColFilter value.
setSort(colId: string, direction: SortDirection, multi?: boolean) => voidUpdates the sorting state of the grid.
toggleSort(colId: string, multi?: boolean) => voidToggles the sort direction of a column.
togglePinned(colId: string, state: PinnedState) => voidToggles the pinned state of a column.
updateData(data: TData[]) => voidUpdates the grid data.
updatePending(newState: boolean) => voidUpdates the pending state.
'use client'

import { useDataGridContext } from '@cerberus/data-grid'

export function BasicDemo() {
  const store = useDataGridContext()
  return <pre>{JSON.stringify(store, null, 2)}</pre>
}
'use client'

import { useDataGridContext } from '@cerberus/data-grid'
import { Text } from '@cerberus/react'
import { useRead } from '@cerberus/signals'
import { Employee } from '../quick-start/data.demo'

export function SignalDemo() {
  const store = useDataGridContext<Employee>()
  const globalFilter = useRead(store.globalFilter)

  return (
    <Text>
      The global filter is: <pre>{JSON.stringify(globalFilter, null, 2)}</pre>
    </Text>
  )
}
'use client'

import { useDataGridContext } from '@cerberus/data-grid'
import { Button } from '@cerberus/react'
import { Employee } from '../quick-start/data.demo'
import { MouseEvent } from 'react'

export function ActionDemo() {
  const store = useDataGridContext<Employee>()

  function handleUpdateData(e: MouseEvent<HTMLButtonElement>) {
    store.togglePinned(e.currentTarget.value, 'left')
  }

  return (
    <Button onClick={handleUpdateData} value="ID">
      Toggle Pinned
    </Button>
  )
}

On this page

  • Overview
  • Using the context
  • Reading Signals
  • Using Actions
  • Store Reference
  • Reactive Values
  • Actions
  • Edit this page on Github