TanStack Table v9:useReactTable が useTable に。使う機能を登録する作りになった

ライブラリの変化@tanstack/react-table 9.3.1
v8 から v9 への置き換え:useReactTable() → useTable({ features })、getCoreRowModel() → 書かない(自動で作られる)

TanStack Table が v9 になり、テーブルを作るフックが useReactTable から useTable に変わりました。いちばん大きな違いは、使う機能を自分で登録する作りになったことです。このサイトのデータグリッドのデモを 8.21.3 から 9.3.1 に移したときの書き換えを、そのまままとめます。

v8 から v9 で変わる書き方

v8v9補足
useReactTable({ … })useTable({ features, … })使う機能を tableFeatures() で登録して渡す
getCoreRowModel: getCoreRowModel()書かない基本の行モデルは自動で作られる
ColumnDef<Employee>ColumnDef<typeof features, Employee>型の1つ目に features が入る
row.getVisibleCells()row.getAllCells()列の表示切替(columnVisibilityFeature)を登録していない場合
flexRender(…)<table.FlexRender header={h} />flexRender も引き続き使える

最小の例

tsx
'use client';
import { useTable, tableFeatures, type ColumnDef } from '@tanstack/react-table';
import { useMemo } from 'react';

interface Employee {
  id: number;
  name: string;
}

const data: Employee[] = [
  { id: 1, name: '田中太郎' },
  { id: 2, name: '佐藤花子' },
];

// 使う機能だけを登録する(この例は基本機能のみ)。
// レンダーのたびに作り直さないよう、コンポーネントの外に置く
const features = tableFeatures({});

export function EmployeeTable() {
  const columns = useMemo<ColumnDef<typeof features, Employee>[]>(() => [
    { accessorKey: 'id', header: 'ID' },
    { accessorKey: 'name', header: '氏名' },
  ], []);

  // getCoreRowModel は不要
  const table = useTable({ features, data, columns });

  return (
    <table>
      <thead>
        {table.getHeaderGroups().map(hg => (
          <tr key={hg.id}>
            {hg.headers.map(h => (
              <th key={h.id}><table.FlexRender header={h} /></th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody>
        {table.getRowModel().rows.map(row => (
          <tr key={row.id}>
            {row.getAllCells().map(cell => (
              <td key={cell.id}><table.FlexRender cell={cell} /></td>
            ))}
          </tr>
        ))}
      </tbody>
    </table>
  );
}

移行で気を付けるところ

  • 機能は登録しないと使えません。 列幅(size や header.getSize())を使うなら tableFeatures({ columnSizingFeature }) のように登録します。このサイトのデモは列幅を使うので、これを登録しています。
  • row.getVisibleCells() は列の表示切替の機能の一部になりました。 columnVisibilityFeature を登録していない構成では、row.getAllCells() を使います。
  • features はコンポーネントの外に置きます。 中に書くと、レンダーのたびに作り直されます(公式の推奨です)。

このサイトで確かめたこと

  • v8 と v9 で、デモの見た目はライト・ダークとも同じでした(画素単位で一致)。
  • 列幅、ダブルクリックでの編集、Enter での確定、Escape での取り消しの動きは変わりません。
  • v8 の useReactTable で出ていた ESLint の react-hooks/incompatible-library の警告は、v9 の useTable では出なくなりました。

動くデモと全体のコードは、データグリッド(Data Grid)の TanStack Table の節にあります。AG Grid・MUI DataGrid などとも並べて比べられます。

動くデモで確かめる

出典

この記事は、2026/11/02 の時点で、上のバージョンで確かめた内容です。