SpreadsheetGrid

Notes for React

ref on React 18 / 19, using a new row right away, render functions, picking the features you ship, testing

Things to keep in mind when using the React version (@ishibashi0112/spreadsheet-grid). The props, column definitions, handle methods and CSS are the same as in the Solid version. What differs is how state is held, how you receive the handle, and when render functions are called (for Solid, see Notes for Solid).

ref and React 18 / 19

You receive the imperative handle (SpreadsheetGridHandle<T>) through the ref prop. When you pass ref to the grid directly, you don't need forwardRef on either React 18.3 or 19 (React 19 receives ref as a prop, and on React 18.3 the grid wraps itself in forwardRef).

import { useRef } from 'react'
import { SpreadsheetGrid, type SpreadsheetGridHandle } from '@ishibashi0112/spreadsheet-grid'

const gridRef = useRef<SpreadsheetGridHandle<Row>>(null)

<SpreadsheetGrid ref={gridRef} /* … */ />
// gridRef.current?.scrollToRow(0)

If you wrap the grid in your own function component (function MyGrid(props) { return <SpreadsheetGrid {...props} /> }) and pass ref to the wrapper, make the wrapper a forwardRef on React 18. React 18 doesn't pass ref as a prop, so the types check but ref.current stays null (not needed on React 19; see the example in Matching a design system).

Using a new row right away (flushSync)

After setRows(...), the grid sees the new rows on the next render. To use a row you just added right away (getViewRowIndex(key) → startEditing(...)), finish the render with flushSync from react-dom first, then look it up.

import { flushSync } from 'react-dom'

const addRow = () => {
  const row = { id: crypto.randomUUID(), name: '' }
  flushSync(() => setRows((prev) => [...prev, row])) // look it up after the grid knows the new row
  const grid = gridRef.current
  const viewRowIndex = grid?.getViewRowIndex(row.id) ?? -1
  const colIndex = grid?.getColIndex('name') ?? -1
  if (viewRowIndex >= 0 && colIndex >= 0) grid?.startEditing(viewRowIndex, colIndex)
}

Render functions and settings that hold functions

  • renderCell / renderHeader and the bar functions (renderTopBar and others) are called every time the grid re-renders. Keep them light, and do heavy work ahead of time with useMemo.
  • Create a text table that holds functions (localeText) outside the component, or wrap it in useMemo (English and text).
  • Call createSpreadsheetGrid from /lite once, at the top of a module. Calling it inside a component creates a different component on every render, and the grid's state (selection, scroll position, values being edited) is lost (reduce the bundle size).

Picking the features you ship (/lite)

SpreadsheetGrid includes every feature (about 236KB gzip of JS loaded up front). createSpreadsheetGrid from @ishibashi0112/spreadsheet-grid/lite builds a grid with only the features you pass, and the rest stay out of your bundle (about 119KB with no features, about 145KB with the standardFeatures preset). The feature names and what they contain are in reduce the bundle size.

Testing

To render with Vitest and jsdom, call installJsdomLayoutStubs from @ishibashi0112/spreadsheet-grid/testing, and interact with @testing-library/react and user-event. When you use fireEvent and write the next action synchronously right after a commit (Enter / Tab), put await act(async () => {}) in between. For tests that open a column filter or the tool panel, call preloadGridPopups() first (Testing).

Next.js and SSR

The grid is a client component that uses hooks. In the App Router, use it from a 'use client' file, and put column definitions that contain functions there too. What the server renders, dark mode, and setting the initial state from the URL are covered in Next.js and SSR.

On this page