react-base-table
A react table component to display large datasets with high performance and flexibility
Top Related Projects
React components for efficiently rendering large lists and tabular data
🤖 Headless UI for building powerful tables & datagrids for TS/JS - React-Table, Vue-Table, Solid-Table, Svelte-Table
Feature-rich and customizable data grid React component
🤖 Headless UI for building powerful tables & datagrids for TS/JS - React-Table, Vue-Table, Solid-Table, Svelte-Table
Datatables for React using Material-UI
Datatable for React based on material-ui's table with additional features
Quick Overview
React Base Table is a powerful and flexible table component for React applications. It provides a virtualized grid system for efficiently rendering large datasets, along with features like column resizing, sorting, and custom cell rendering. The library aims to offer a balance between performance and customization for building complex table interfaces.
Pros
- High performance with virtualization for handling large datasets
- Extensive customization options for cell rendering and styling
- Built-in support for common table features like sorting and column resizing
- TypeScript support for improved developer experience
Cons
- Steeper learning curve compared to simpler table libraries
- Documentation could be more comprehensive, especially for advanced use cases
- Limited built-in theming options, requiring more custom CSS
- Some users report occasional issues with scrolling behavior
Code Examples
- Basic table setup:
import BaseTable, { Column } from 'react-base-table'
const MyTable = () => (
<BaseTable
data={myData}
width={800}
height={400}
>
<Column key="name" dataKey="name" title="Name" width={200} />
<Column key="age" dataKey="age" title="Age" width={100} />
<Column key="address" dataKey="address" title="Address" width={300} />
</BaseTable>
)
- Custom cell renderer:
const CustomCell = ({ cellData }) => (
<div style={{ color: cellData > 30 ? 'red' : 'green' }}>
{cellData}
</div>
)
<Column
key="age"
dataKey="age"
title="Age"
width={100}
cellRenderer={CustomCell}
/>
- Implementing sorting:
const [sortBy, setSortBy] = useState({ key: 'name', order: 'asc' })
<BaseTable
data={sortedData}
sortBy={sortBy}
onColumnSort={({ key, order }) => setSortBy({ key, order })}
>
{/* Column definitions */}
</BaseTable>
Getting Started
-
Install the package:
npm install react-base-table
-
Import and use in your React component:
import React from 'react' import BaseTable, { Column } from 'react-base-table' import 'react-base-table/styles.css' const MyTable = ({ data }) => ( <BaseTable data={data} width={1000} height={400}> <Column key="id" dataKey="id" title="ID" width={100} /> <Column key="name" dataKey="name" title="Name" width={200} /> <Column key="email" dataKey="email" title="Email" width={300} /> </BaseTable> ) export default MyTable
-
Make sure to include the CSS file in your project for proper styling.
Competitor Comparisons
React components for efficiently rendering large lists and tabular data
Pros of react-virtualized
- More comprehensive feature set, including support for various virtualized components (List, Grid, Table, etc.)
- Better performance for large datasets due to advanced windowing techniques
- Extensive documentation and examples
Cons of react-virtualized
- Steeper learning curve due to its complexity and numerous configuration options
- Larger bundle size, which may impact initial load times
- Less frequent updates and maintenance compared to react-base-table
Code Comparison
react-virtualized:
import { List } from 'react-virtualized';
<List
width={300}
height={300}
rowCount={1000}
rowHeight={20}
rowRenderer={({ key, index, style }) => (
<div key={key} style={style}>Row {index}</div>
)}
/>
react-base-table:
import BaseTable from 'react-base-table';
<BaseTable
width={300}
height={300}
data={data}
columns={columns}
/>
react-virtualized offers more granular control over rendering and optimization, while react-base-table provides a simpler API for basic table functionality. The choice between the two depends on the specific requirements of your project, such as dataset size, performance needs, and desired customization level.
🤖 Headless UI for building powerful tables & datagrids for TS/JS - React-Table, Vue-Table, Solid-Table, Svelte-Table
Pros of TanStack Table
- Framework-agnostic, supporting React, Vue, Solid, and more
- Highly flexible and customizable with a powerful plugin system
- Excellent TypeScript support and type safety
Cons of TanStack Table
- Steeper learning curve due to its extensive API and configuration options
- Requires more setup and boilerplate code for basic use cases
Code Comparison
react-base-table:
import BaseTable, { Column } from 'react-base-table'
<BaseTable
data={data}
width={600}
height={400}
>
<Column key="name" dataKey="name" width={100} />
<Column key="age" dataKey="age" width={100} />
</BaseTable>
TanStack Table:
import { useReactTable, getCoreRowModel, flexRender } from '@tanstack/react-table'
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
})
return (
<table>
{table.getHeaderGroups().map(headerGroup => (
<tr key={headerGroup.id}>
{headerGroup.headers.map(header => (
<th key={header.id}>
{flexRender(header.column.columnDef.header, header.getContext())}
</th>
))}
</tr>
))}
{/* ... body rendering */}
</table>
)
Feature-rich and customizable data grid React component
Pros of react-data-grid
- More comprehensive feature set, including built-in sorting, filtering, and cell editing
- Better documentation and examples, making it easier for developers to get started
- Larger community and more frequent updates, potentially leading to better long-term support
Cons of react-data-grid
- Heavier package size, which may impact performance for larger datasets
- Less flexible styling options compared to react-base-table
- Steeper learning curve due to more complex API and configuration options
Code Comparison
react-data-grid:
import ReactDataGrid from 'react-data-grid';
const columns = [
{ key: 'id', name: 'ID' },
{ key: 'title', name: 'Title' }
];
const rows = [
{ id: 1, title: 'Example 1' },
{ id: 2, title: 'Example 2' }
];
function MyGrid() {
return <ReactDataGrid columns={columns} rows={rows} />;
}
react-base-table:
import BaseTable from 'react-base-table';
const columns = [
{ key: 'id', title: 'ID', dataKey: 'id', width: 100 },
{ key: 'title', title: 'Title', dataKey: 'title', width: 200 }
];
const data = [
{ id: 1, title: 'Example 1' },
{ id: 2, title: 'Example 2' }
];
function MyTable() {
return <BaseTable columns={columns} data={data} />;
}
🤖 Headless UI for building powerful tables & datagrids for TS/JS - React-Table, Vue-Table, Solid-Table, Svelte-Table
Pros of TanStack Table
- Framework-agnostic, supporting React, Vue, Solid, and more
- Highly flexible and customizable with a powerful plugin system
- Excellent TypeScript support and type safety
Cons of TanStack Table
- Steeper learning curve due to its extensive API and configuration options
- Requires more setup and boilerplate code for basic use cases
Code Comparison
react-base-table:
import BaseTable, { Column } from 'react-base-table'
<BaseTable
data={data}
width={600}
height={400}
>
<Column key="name" dataKey="name" width={100} />
<Column key="age" dataKey="age" width={100} />
</BaseTable>
TanStack Table:
import { useReactTable, getCoreRowModel, flexRender } from '@tanstack/react-table'
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
})
return (
<table>
{table.getHeaderGroups().map(headerGroup => (
<tr key={headerGroup.id}>
{headerGroup.headers.map(header => (
<th key={header.id}>
{flexRender(header.column.columnDef.header, header.getContext())}
</th>
))}
</tr>
))}
{/* ... body rendering */}
</table>
)
Datatables for React using Material-UI
Pros of mui-datatables
- Built-in Material-UI integration, offering a polished and consistent UI out of the box
- Extensive built-in features like filtering, sorting, and pagination
- Responsive design with mobile-friendly view options
Cons of mui-datatables
- Less flexible for custom styling and layout compared to react-base-table
- Potentially heavier bundle size due to Material-UI dependencies
- May have a steeper learning curve for developers unfamiliar with Material-UI
Code Comparison
mui-datatables:
import MUIDataTable from "mui-datatables";
const columns = ["Name", "Company", "City", "State"];
const data = [
["Joe James", "Test Corp", "Yonkers", "NY"],
["John Walsh", "Test Corp", "Hartford", "CT"],
];
<MUIDataTable
title={"Employee List"}
data={data}
columns={columns}
options={options}
/>
react-base-table:
import BaseTable, { Column } from 'react-base-table'
<BaseTable data={data} width={600} height={400}>
<Column key="name" dataKey="name" title="Name" width={100} />
<Column key="age" dataKey="age" title="Age" width={100} />
<Column key="address" dataKey="address" title="Address" width={200} />
</BaseTable>
The code comparison shows that mui-datatables offers a more concise setup, while react-base-table provides more granular control over column definitions.
Datatable for React based on material-ui's table with additional features
Pros of material-table
- More comprehensive out-of-the-box features, including sorting, filtering, and pagination
- Built-in Material-UI integration for a polished look and feel
- Extensive documentation and examples for easier implementation
Cons of material-table
- Larger bundle size due to additional features and dependencies
- Less flexibility for custom styling compared to react-base-table
- May have a steeper learning curve for developers new to Material-UI
Code Comparison
material-table:
import MaterialTable from 'material-table';
<MaterialTable
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Age', field: 'age', type: 'numeric' }
]}
data={[{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }]}
/>
react-base-table:
import BaseTable, { Column } from 'react-base-table';
<BaseTable data={[{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }]}>
<Column key="name" dataKey="name" title="Name" />
<Column key="age" dataKey="age" title="Age" />
</BaseTable>
material-table offers a more concise API with built-in features, while react-base-table provides a more flexible approach for custom implementations. The choice between the two depends on project requirements, desired customization level, and integration with existing UI frameworks.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-base-table
BaseTable is a react table component to display large datasets with high performance and flexibility
Install
# npm
npm install react-base-table --save
# yarn
yarn add react-base-table
Usage
import BaseTable, { Column } from 'react-base-table'
import 'react-base-table/styles.css'
// Important: if you fail to import react-base-table/styles.css then
// BaseTable will not render as advertised in the included examples.
// For advanced styling see link below:
// https://github.com/Autodesk/react-base-table#advance
...
<BaseTable data={data} width={600} height={400}>
<Column key="col0" dataKey="col0" width={100} />
<Column key="col1" dataKey="col1" width={100} />
...
</BaseTable>
...
Learn more at the website
unique key
key
is required for column definition or the column will be ignored
Make sure each item in data
is unique by a key, the default key is id
, you can customize it via rowKey
size
width
is required for column definition, but in flex mode(fixed={false}
), you can set width={0}
and flexGrow={1}
to achieve flexible column width, checkout the Flex Column example
width
and height
(or maxHeight
) are required to display the table properly
In the examples
we are using a wrapper const Table = props => <BaseTable width={700} height={400} {...props} />
to do that
If you want it responsive, you can use the AutoResizer
to make the table fill the container, checkout the Auto Resize example
closure problem in custom renderers
In practice we tend to write inline functions for custom renderers, which would make shouldUpdateComponent
always true as the inline function will create a new instance on every re-render, to avoid "unnecessary" re-renders, BaseTable
ignores functions when comparing column definition by default, it works well in most cases before, but if we use external data instead of reference state in custom renderers, we always get the staled initial value although the data has changed
It's recommended to inject the external data in column definition to solve the problem, like <Column foo={foo} bar={bar} cellRenderer={({ column: { foo, bar }}) => { ... } } />
, the column definition will update on external data change, with this pattern we can easily move the custom renderers out of column definition for sharing, the downside is it would bloat the column definition and bug prone
Things getting worse with the introduction of React hooks, we use primitive state instead of this.state
, so it's easy to encounter the closure problem, but with React hooks, we can easily memoize functions via useCallback
or useMemo
, so the implicit optimization could be replaced with user land optimization which is more intuitive, to turn off the implicit optimization, set ignoreFunctionInColumnCompare
to false
which is introduced since v1.11.0
Here is an example to demonstrate
Browser Support
BaseTable
is well tested on all modern browsers and IE11. You have to polyfill Array.prototype.findIndex
to make it works on IE
The examples don't work on IE as they are powered by react-runner which is a react-live
like library but only for modern browsers.
Advance
BaseTable is designed to be the base component to build your own complex table component
Styling
The simplest way is overriding the default styles (assuming you are using scss
)
// override default variables for BaseTable
$table-prefix: AdvanceTable;
$table-font-size: 13px;
$table-padding-left: 15px;
$table-padding-right: 15px;
$column-padding: 7.5px;
...
$show-frozen-rows-shadow: false;
$show-frozen-columns-shadow: true;
@import '~react-base-table/es/_BaseTable.scss';
.#{$table-prefix} {
&:not(.#{$table-prefix}--show-left-shadow) {
.#{$table-prefix}__table-frozen-left {
box-shadow: none;
}
}
&:not(.#{$table-prefix}--show-right-shadow) {
.#{$table-prefix}__table-frozen-right {
box-shadow: none;
}
}
...
}
You can write your own styles from scratch or use CSS-in-JS solutions to achieve that
Custom components
<BaseTable
classPrefix="AdvanceTable"
components={{
TableCell: AdvanceTableCell,
TableHeaderCell: AdvanceTableHeaderCell,
ExpandIcon: AdvanceExpandIcon,
SortIndicator: AdvanceSortIndicator,
}}
...
/>
Custom renderers & props
There are a lot of highly flexible props like xxxRenderer
and xxxProps
for you to build your own table component, please check the api and examples for more details
Example
We are using a advanced table component based on BaseTable
internally, with much more features, including row selection, row grouping, data aggregation, column settings, column reordering, and column grouping, tooltip, inline editing.
Development
We use Yarn
as the package manager, checkout this repo and run yarn
under both root folder and website
folder in install packages, then run yarn start
to start the demo site powered by Gatsby
Contributing
Please check guidelines for more details
Top Related Projects
React components for efficiently rendering large lists and tabular data
🤖 Headless UI for building powerful tables & datagrids for TS/JS - React-Table, Vue-Table, Solid-Table, Svelte-Table
Feature-rich and customizable data grid React component
🤖 Headless UI for building powerful tables & datagrids for TS/JS - React-Table, Vue-Table, Solid-Table, Svelte-Table
Datatables for React using Material-UI
Datatable for React based on material-ui's table with additional features
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot