Table

Table component is used to organize and display data efficiently. It renders a `<table>` element by default

Source@chakra-ui/react

Import#

import { Table } from '@chakra-ui/react'

Usage#

<Table.Root>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Name</Table.ColumnHeader>
<Table.ColumnHeader>Email</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>Segun Adebayo</Table.Cell>
<Table.Cell>sage@acme.org </Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Sally Thompson</Table.Cell>
<Table.Cell>sally@acme.org</Table.Cell>
</Table.Row>
</Table.Body>
</Table.Root>

Driving the table with data#

To render a table with data, you can use the map function to iterate over the data and render the table rows.

const data = [
{ id: 1, name: 'John Doe', age: 25, gender: 'Male' },
{ id: 2, name: 'Jane Smith', age: 30, gender: 'Female' },
{ id: 3, name: 'Michael Johnson', age: 35, gender: 'Male' },
{ id: 4, name: 'Emily Davis', age: 28, gender: 'Female' },
{ id: 5, name: 'David Brown', age: 32, gender: 'Male' },
]
function Demo() {
const rows = data.map((row) => (
<Table.Row key={row.id}>
<Table.Cell>{row.name}</Table.Cell>
<Table.Cell numeric>{row.age}</Table.Cell>
<Table.Cell>{row.gender}</Table.Cell>
</Table.Row>
))
return (
<Table.Root>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Name</Table.ColumnHeader>
<Table.ColumnHeader numeric>Age</Table.ColumnHeader>
<Table.ColumnHeader>Gender</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>{rows}</Table.Body>
</Table.Root>
)
}
render(<Demo />)

Changing the table variant#

The table component comes in 2 variants: line and outline

<Table.Root variant='outline'>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>To convert</Table.ColumnHeader>
<Table.ColumnHeader>into</Table.ColumnHeader>
<Table.ColumnHeader numeric>multiply by</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>inches</Table.Cell>
<Table.Cell>millimetres (mm)</Table.Cell>
<Table.Cell numeric>25.4</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>feet</Table.Cell>
<Table.Cell>centimetres (cm)</Table.Cell>
<Table.Cell numeric>30.48</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>yards</Table.Cell>
<Table.Cell>metres (m)</Table.Cell>
<Table.Cell numeric>0.91444</Table.Cell>
</Table.Row>
</Table.Body>
<Table.Footer>
<Table.Row>
<Table.Cell>To convert</Table.Cell>
<Table.Cell>into</Table.Cell>
<Table.Cell numeric>multiply by</Table.Cell>
</Table.Row>
</Table.Footer>
</Table.Root>

Changing the table size#

The table component is available in 3 sizes: sm, md, lg. The default size is md.

<For each={['sm', 'md', 'lg']}>
{(size) => (
<Table.Overflow key={size} mb='10'>
<Table.Root size={size}>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Name</Table.ColumnHeader>
<Table.ColumnHeader>Email</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>Segun Adebayo</Table.Cell>
<Table.Cell>sage@acme.org </Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Sally Thompson</Table.Cell>
<Table.Cell>sally@acme.org</Table.Cell>
</Table.Row>
</Table.Body>
</Table.Root>
</Table.Overflow>
)}
</For>

Handling overflow in tables#

Tables can be quite large and can overflow the parent container. To handle overflow, you can wrap the table in a Table.Overflow component.

Performance optimization in large tables#

Due to the limitations of the DOM combined with CSS-in-JS, large tables can be slow to render. To optimize style performance, pass the native prop to the Table.Root component and render the table using native HTML elements.

<Table.Root native>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Segun Adebayo</td>
<td>sage@acme.org </td>
</tr>
<tr>
<td>Sally Thompson</td>
<td>sally@acme.org</td>
</tr>
</tbody>
</Table.Root>
Edit this page on GitHub

Proudly made inNigeria by Segun Adebayo

Deployed by â–² Vercel