gridcn2.0.0
Row examples

Streaming rows example

Push batches of cell updates into a live ticker four times a second and choose between an immediate and a deferred re-sort.

A live ticker receives new values with updateCells, four batches a second. The Auto-sort switch sets the reorder option of each batch. With "immediate", the grid re-sorts on every batch. With "defer", the rows keep their positions. If a batch changes a value that the sort reads, a Re-sort bar appears.

Click the Change % header to sort: with Auto-sort off, rows hold their position as new values stream in and a "Re-sort" bar appears once the order goes stale; with Auto-sort on, the grid re-sorts on every tick (4/s).

Symbol
Price
Change %
Volume
AAPL
100
0
1000000
MSFT
117.5
0
1025000
NVDA
135
0
1050000
AMZN
152.5
0
1075000
GOOG
170
0
1100000
META
187.5
0
1125000
TSLA
205
0
1150000
AMD
222.5
0
1175000
INTC
240
0
1200000
NFLX
257.5
0
1225000
pnpm dlx shadcn@latest add @gridcn/data-grid-streaming-demo

What the demo shows

  • A feed patches the change and volume columns of every row every 250 ms. The Pause feed button stops the feed and the Start feed button resumes it.
  • Auto-sort starts off. reorder defaults to "defer".
  • Each batch reports a change with source: "stream", which undo history skips and onSave never receives.
  • Click the Change % header to sort. A batch that touches no sorted or filtered column never reorders the rows and never shows the Re-sort bar.
  • With Auto-sort on, the rows shuffle as the Change % values cross.
  • With Auto-sort off, the rows hold their positions. When a batch changes a Change % value, the Re-sort bar appears. Its button calls reconcileView().

Wire the feed

Render both components inside the provider. The feed patches by row id and column id. The bar reads useDataGridViewStale:

import { useEffect } from "react";
import {
  DataGridBody,
  DataGridHeader,
  DataGridProvider,
  DataGridRoot,
  defineColumns,
  useDataGridActions,
  useDataGridViewStale,
  type CellPatch,
} from "@/components/data-grid/data-grid";
import { Button } from "@/components/ui/button";

type Ticker = { id: string; change: number };

const columns = defineColumns<Ticker>()([
  { id: "id", header: "Symbol", accessorKey: "id" },
  { id: "change", header: "Change %", accessorKey: "change", type: "number" },
]);

const rows: Ticker[] = [
  { id: "AAPL", change: 0 },
  { id: "MSFT", change: 0 },
];

function TickerFeed({ autoSort }: { autoSort: boolean }) {
  const actions = useDataGridActions();

  useEffect(() => {
    const timer = setInterval(() => {
      const patches: CellPatch[] = rows.map((row) => ({
        rowId: row.id,
        columnId: "change",
        value: Number((Math.random() * 4 - 2).toFixed(2)),
      }));
      actions.updateCells(patches, {
        source: "stream",
        reorder: autoSort ? "immediate" : "defer",
      });
    }, 250);
    return () => clearInterval(timer);
  }, [actions, autoSort]);

  return null;
}

function ReSortBar() {
  const actions = useDataGridActions();
  const viewStale = useDataGridViewStale();
  if (!viewStale) return null;
  return (
    <Button size="sm" variant="outline" onClick={() => actions.reconcileView()}>
      Re-sort
    </Button>
  );
}

function TickerGrid({ autoSort }: { autoSort: boolean }) {
  return (
    <DataGridProvider
      defaultData={rows}
      columns={columns}
      getRowId={(row) => row.id}
      headerClickBehavior="sort"
    >
      <TickerFeed autoSort={autoSort} />
      <ReSortBar />
      <DataGridRoot>
        <DataGridHeader />
        <DataGridBody />
      </DataGridRoot>
    </DataGridProvider>
  );
}

Feed at a high rate

A feed of 1,000 updates a second must not make 1,000 grid calls a second. This demo holds the patches in a buffer and sends them in one updateCells call per animation frame. The grid has 100,000 rows. The feed touches the first 200.

Frames per second
—
p95 frame time (ms)
—
Applied cells per second
—
Grid calls per second
—
Symbol
Price
Change %
Volume
SYM000000
298.53
-1.61
542291
SYM000001
493.55
1.38
529202
SYM000002
434.95
3.32
74559
SYM000003
498.69
-2.32
193354
SYM000004
496.38
-3.53
384064
SYM000005
15.13
-3.42
673684
SYM000006
462.01
2.48
741223
SYM000007
31.86
0.94
495958
SYM000008
101.95
3.14
68913
SYM000009
401.37
-2.83
27205
SYM000010
358.24
1.91
863212
SYM000011
408.81
2.69
742507
SYM000012
18.1
-1.29
150273
SYM000013
329.43
-0.94
299411
SYM000014
395.09
-1.5
874448
SYM000015
279.04
-2.4
462005
SYM000016
287.03
-0.68
298336
SYM000017
420.13
0.81
353265
SYM000018
358.72
0.69
399464
SYM000019
286.87
-2.1
86249
SYM000020
295.64
0.2
454704
SYM000021
147.5
-0.38
79647
SYM000022
385.59
-2.38
120651
SYM000023
425.09
0.83
617568
SYM000024
466.11
-2.01
480432
SYM000025
196.76
1.05
680217
SYM000026
115.82
-2.46
617333
SYM000027
0.18
-1.73
776942
SYM000028
383.16
-0.54
367157
SYM000029
254.64
-1.97
292380
pnpm dlx shadcn@latest add @gridcn/data-grid-stream-rate-demo
  • Pause stops the feed. The feed also stops while the tab is hidden or the demo is off screen.
  • The rate select sets how many patches the feed makes each second. The grid calls per second stay at or below the frames per second.
  • The reorder select sets the reorder option of each call. Click a column header to sort, then compare the three values.
  • The Flash switch sets flash on each call.
  • The counters update once a second. Applied cells counts the writes that changed a value.

Flush the buffer from an animation frame:

import { useEffect } from "react";
import {
  useDataGridActions,
  type CellPatch,
} from "@/components/data-grid/data-grid";

function FrameFeed({ buffer }: { buffer: CellPatch[] }) {
  const actions = useDataGridActions();

  useEffect(() => {
    let frame = 0;
    const flush = () => {
      if (buffer.length > 0) {
        const patches = buffer.splice(0);
        actions.updateCells(patches, { source: "stream", reorder: "frame" });
      }
      frame = requestAnimationFrame(flush);
    };
    frame = requestAnimationFrame(flush);
    return () => cancelAnimationFrame(frame);
  }, [actions, buffer]);

  return null;
}

On this page