Blog>
Snippets

Creating Custom Cell Renderers in React TanStack Table

Demonstrates how to create and implement a custom cell renderer that formats dates in a 'DD/MM/YYYY' format, using moment.js for a React TanStack Table column.
import { createColumnHelper } from '@tanstack/react-table';
import moment from 'moment';
Import the necessary libraries. 'createColumnHelper' from TanStack Table for creating columns and 'moment' for date formatting.
const columnHelper = createColumnHelper();
Initialize the column helper.
const columns = [
  columnHelper.accessor('dateColumn', {
    header: 'Date',
    cell: info => moment(info.getValue()).format('DD/MM/YYYY'),
  }),
];
Define a column for the table where 'dateColumn' is assumed to be the field in your data representing the date. The 'cell' property uses a custom renderer that formats the date using moment.js.