MRT logoMantine React Table

On This Page

    TypeScript Usage

    TanStack Table itself is written in TypeScript, and Mantine React Table builds on top of its great type definitions for best in class TypeScript experience.
    If, however, you cannot use TypeScript in your project for some reason, checkout down below for how to use JSDoc instead of TypeScript to get the same type hints.

    Is TypeScript Required?

    No, TypeScript is not required to use Mantine React Table. You can just use JavaScript and everything will work just fine, but you will be missing out on a lot of great type hints and type safety that can help you build your app faster and with less bugs.

    Define You TData Type

    Mantine React Table makes use of generics to make working with your specific row data structures easier. You will see that most of the MRT_* types that you can use accept a TData generic.
    Let's say that the data in your table is an array of users that looks like this:
    const data: User[] = [
    { id: 1, name: 'John', age: 23 },
    { id: 2, name: 'Alice', age: 17 },
    { id: 3, name: 'Bob', age: 32 },
    ];
    Then your TData type can be defined as:
    export type User = {
    id: number;
    name: string;
    age: number;
    };

    Define Your Column Definitions with MRT_ColumnDef

    Mantine React Table provides you with a MRT_ColumnDef type that you can use to define your column definitions. It is a generic type that accepts your TData type as a generic.
    import {
    MantineReactTable,
    useMantineReactTable,
    type MRT_ColumnDef, // <--- import MRT_ColumnDef
    } from '@mantine/react-table';
    import { type User } from './types'; // <--- import your TData type from wherever you defined it
    // define your columns, pass User as a generic to MRT_ColumnDef
    const columns: MRT_ColumnDef<User>[] = [
    {
    accessorKey: 'id', //you should get type hints for all of your keys if you defined your TData type correctly
    header: 'ID',
    enableSorting: false, //you should get type hints for all possible column options that you can define here
    },
    {
    accessorKey: 'name',
    header: 'Name',
    },
    {
    accessorFn: (originalRow) => Number(originalRow.age), //you should also get type hints for your accessorFn
    header: 'Age',
    },
    ];

    Use JSDoc instead of TypeScript

    If you are in a situation where you are not able to install TypeScript in your project, you can technically do the same thing as up above in JavaScript using JSDoc.
    import { MantineReactTable, useMantineReactTable } from '@mantine/react-table';
    //define TData type with JSDoc
    /**
    * @typedef {Object} User
    * @property {number} id
    * @property {string} name
    * @property {number} age
    */
    //import MRT_ColumnDef type with JSDoc
    /**
    * @type {import('mantine-react-table').MRT_ColumnDef<User>[]}
    */
    const columns = [
    {
    accessorKey: 'id', //you should get type hints for all of your keys if you defined your TData type correctly
    header: 'ID',
    enableSorting: false, //you should get type hints for all possible column options that you can define here
    },
    {
    accessorKey: 'name',
    header: 'Name',
    },
    {
    accessorFn: (originalRow) => Number(originalRow.age), //you should also get type hints for your accessorFn
    header: 'Age',
    },
    ];
    You can help make these docs better! PRs are Welcome
    Using Material-UI instead of Mantine?
    Check out Material React Table