DocsBlog
  • 1.2.1

  • light

    dark

    system

    Switch mode
  • Cerberus

    Acheron

    Elysium

Get Started
Components
Data Grid
Signals
Styling
Theming

Get Started

OverviewQuick StartColumnsContextTheme

Layout

DimensionsSizingSlotsOverlaysNewToolbarFooterPagination

Features

PinningSortingFilteringNew

Reference

API

On this page

Loading...

Loading...

Loading...

Loading...

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
columnsReadonlyAccessor<InternalColumn<TData>[]>A list of processed columns.
rowsReadonlyAccessor<TData[]>A list of processed rows.
rowCountReadonlyAccessor<number>The current number of rows in the grid.
rowSizeReadonlyAccessor<number>The height of the row.
pendingReadonlyAccessor<boolean>The state of the pending prop of the Data Grid.
hasSkeletonReadonlyAccessor<boolean>If a skeleton overlay option is provided.
globalFilterReadonlyAccessor<BaseFilterState>The current global filter value used for column filtering.
collFiltersReadonlyAccessor<ColumnFilterState>The current column filters used.
showColFilterReadonlyAccessor<boolean>If the user has chosen the column filter feature option.
sortingReadonlyAccessor<SortState[]>The current sorting state of the grid.
pageCountReadonlyAccessor<number>The current page count of the grid.
pageIndexReadonlyAccessor<number>The current page index of the grid.
pageSizeReadonlyAccessor<number>The current page size of the grid.
pageRangeReadonlyAccessor<number[]>The page range options of the grid.
currentPageSizeReadonlyAccessor<number>The current page range of the grid.
isServerPaginatedReadonlyAccessor<boolean>If pagination is using server-based.
rootCssVarsReadonlyAccessor<CSSProperties>The current CSS variables for the grid root element.
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.
setPageSize(page: number) => voidUpdates the page size of the grid.
setGlobalFilter(val: BaseFilterState) => voidUpdates the global filter value.
setColFilterSetter<ColumnFilterState>Updates the col filter value.
setShowColFilter(val: boolean) => voidUpdates the showColFilter 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.
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>
  )
}