DocsBlog
  • 1.0.0

  • light

    dark

    system

    Switch mode
  • Cerberus

    Acheron

    Elysium

Get Started
Components
Data Grid
Styling
Theming

Get Started

OverviewQuick StartColumnsContext

Layout

DimensionsSizingToolbarPagination

Features

PinningSorting

Reference

API

On this page

  • Edit this page on Github

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.
rowsAccessor<TData[]>A list of processed rows.
globalFilterAccessor<string>The current global filter value used for column filtering.
sortingAccessor<SortState[]>The current sorting state of the grid.
rootCssVarsReadonlyAccessor<CSSProperties>The current CSS variables for the grid root element.
rowCountReadonlyAccessor<number>The current number of rows in the grid.
totalWidthReadonlyAccessor<number>The total width of the grid (used for pinning).
visibleRowsReadonlyAccessor<TData[]>The list of visible rows in the viewport.

Actions

NameTypeDescription
resizeColumn(columnId: string, delta: number) => voidResizes a column by updating its width.
setContainerWidth(width: number) => voidUpdates the width of the grid container.
setPage(page: number) => voidUpdates the current page of the grid.
setGlobalFilter(val: string) => voidUpdates the global filter value.
setSort(colId: string, direction: SortDirection, multi?: boolean) => voidUpdates the sorting state of the grid.
togglePinned(colId: string, state: PinnedState) => voidToggles the pinned state of a column.
toggleSort(colId: string, multi?: boolean) => voidToggles the sort direction of a column.
updateData(data: TData[]) => voidUpdates the grid data.
Cerberus Design System | Docs
'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: {globalFilter}</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>
  )
}