ShakibDShy

Basic Table Example

Learn how to create a simple table with React Table Grid

This example demonstrates how to create a basic table using React Table Grid. It's the perfect starting point for beginners to understand the core concepts.

Overview

The basic table example shows how to:

  • Set up a basic table structure
  • Define typed columns
  • Handle basic state changes
  • Configure table height

Example

Name
Age
Email
Adrian Brown
38
adrian.b@company.com
Alexander Davis
37
alex.d@company.com
Amelia Moore
35
amelia.m@company.com
Andrew Wilson
44
andrew.w@company.com
Aria Davis
31
aria.d@company.com
Aurora White
32
aurora.w@company.com
Ava Martinez
33
ava.m@company.com
Benjamin Taylor
39
ben.t@company.com
Charlotte Wilson
31
charlotte.w@company.com
Christopher Moore
38
chris.m@company.com
Daniel White
35
daniel.w@company.com
David Lee
37
david.l@company.com
Elena Anderson
34
elena.a@company.com
Elijah Thompson
40
elijah.t@company.com
Emily Davis
29
emily.d@company.com
Emma Rodriguez
30
emma.r@company.com
Ethan Anderson
40
ethan.a@company.com
Felix Wilson
41
felix.w@company.com
Gabriel Martinez
37
gabriel.m@company.com
Hazel Garcia
33
hazel.g@company.com
Henry Garcia
43
henry.g@company.com
Isabella Thompson
31
isabella.t@company.com
Jack Thompson
41
jack.t@company.com
James Taylor
39
james.t@company.com
Jane Smith
34
jane.smith@company.com
Jennifer Martinez
36
jennifer.m@company.com
John Doe
28
john.doe@company.com
Leo Wilson
42
leo.w@company.com
Lily Moore
33
lily.m@company.com
Lisa Anderson
33
lisa.a@company.com
Lucas Brown
37
lucas.b@company.com
Luna Rodriguez
30
luna.r@company.com
Matthew Brown
36
matthew.b@company.com
Max Lee
36
max.l@company.com
Maya Rodriguez
34
maya.r@company.com
Mia Johnson
34
mia.j@company.com
Michael Wilson
45
michael.w@company.com
Noah Clark
39
noah.c@company.com
Olivia Garcia
29
olivia.g@company.com
Owen Martinez
38
owen.m@company.com
Robert Johnson
41
robert.j@company.com
Ruby Anderson
31
ruby.a@company.com
Sarah Brown
31
sarah.b@company.com
Scarlett White
33
scarlett.w@company.com
Sebastian Clark
36
sebastian.c@company.com
Sophia Lee
32
sophia.l@company.com
Stella Davis
35
stella.d@company.com
Victoria Lee
32
victoria.l@company.com
William Clark
42
william.c@company.com
Zoe Garcia
35
zoe.g@company.com

Step-by-Step Guide

1. Define Data Types

First, define the shape of your data using TypeScript:

type DataItem = {
  id: number;
  name: string;
  age: number;
  email: string;
};

2. Create Column Helper

Use createColumnHelper to get type-safe column definitions:

const columnHelper = createColumnHelper<DataItem>();

3. Define Columns

Create your column definitions using the column helper:

const columns: Column<DataItem>[] = [
  columnHelper.accessor("name", {
    header: "Name",
    sortable: false,
  }),
  // ... more columns
];

4. Create Table Component

Implement the table component with the required props:

<TableGrid
  columns={columns}
  data={data}
  maxHeight="400px"
/>

Core Props Explained

PropTypeRequiredDescription
columnsColumn<T>[]YesColumn definitions
dataT[]YesTable data array
maxHeightstringNoMaximum table height
onStateChangefunctionNoState change callback

Basic Customization

1. Styling

Add basic styling using the styleConfig prop:

<TableGrid
  styleConfig={{
    container: {
      className: "border rounded-lg shadow-sm",
    },
    row: {
      className: "hover:bg-gray-50",
    },
  }}
/>

2. Height Configuration

Control table height:

<TableGrid
  maxHeight="400px"  // Fixed height
  // or
  maxHeight="100vh"  // Viewport relative
/>

State Management

Basic State Tracking

Monitor table state changes:

<TableGrid
  onStateChange={(state) => {
    console.log("Current state:", state);
    // Access:
    // - state.data (current data)
    // - state.sortColumn (current sort column)
    // - state.sortDirection (current sort direction)
  }}
/>

Best Practices

  1. Type Safety

    • Always define proper TypeScript interfaces
    • Use createColumnHelper for type-safe columns
  2. Performance

    • Keep data structures flat when possible
    • Avoid unnecessary re-renders
    • Consider memoization for large datasets
  3. Code Organization

    • Separate column definitions from component logic
    • Use consistent naming conventions
    • Document complex configurations

Common Issues and Solutions

Table Not Rendering

If the table isn't rendering:

  1. Check that columns and data props are properly defined
  2. Verify data structure matches column accessors
  3. Ensure container has proper dimensions

Type Errors

If you're getting TypeScript errors:

  1. Verify your data type definitions
  2. Check column accessor keys match data properties
  3. Ensure proper generic types are passed to components

Last updated on