Skip to content

useTable Hook

For full control over the table markup, use useTable directly. It provides all the logic while you control the rendering.

tsx
import { useTable } from '@forjedio/inertia-table-react';
import { flexRender } from '@tanstack/react-table';

function CustomTable({ tableData }) {
    const {
        table,          // TanStack table instance
        columns,        // BuiltColumn[] for simple iteration
        searchTerm,
        onSearch,
        onPageChange,
    } = useTable({ tableData });

    return (
        <div>
            <input
                value={searchTerm}
                onChange={(e) => onSearch(e.target.value)}
            />
            <table>
                <thead>
                    {table.getHeaderGroups().map((hg) => (
                        <tr key={hg.id}>
                            {hg.headers.map((h) => (
                                <th key={h.id}>
                                    {flexRender(h.column.columnDef.header, h.getContext())}
                                </th>
                            ))}
                        </tr>
                    ))}
                </thead>
                <tbody>
                    {table.getRowModel().rows.map((row) => (
                        <tr key={row.id}>
                            {row.getVisibleCells().map((cell) => (
                                <td key={cell.id}>
                                    {flexRender(cell.column.columnDef.cell, cell.getContext())}
                                </td>
                            ))}
                        </tr>
                    ))}
                </tbody>
            </table>
        </div>
    );
}
vue
<!-- Coming soon -->

Return Value

PropertyTypeDescription
tableTanStack Table<Row>Full TanStack table instance
columnsBuiltColumn[]Simple array with renderHeader() and renderCell()
classNamesClassNamesMerged default + custom class names
searchTermstringCurrent search value
onSearch(term) => voidDebounced search handler
sortBystring | nullActive sort column
sortDir'asc' | 'desc'Sort direction
onSort(sortKey) => voidSort toggle handler
getSortState(sortKey) => SortStateGet sort state for a column
onPageChange(page) => voidNavigate to page

Simple Iteration

If you don't need TanStack features, use columns directly:

tsx
const { columns } = useTable({ tableData });

<table>
    <thead>
        <tr>
            {columns.map((col) => <th key={col.id}>{col.renderHeader()}</th>)}
        </tr>
    </thead>
    <tbody>
        {tableData.data.map((row, i) => (
            <tr key={row.id}>
                {columns.map((col) => <td key={col.id}>{col.renderCell(row, i)}</td>)}
            </tr>
        ))}
    </tbody>
</table>
vue
<!-- Coming soon -->