React Table Tutorial: Building Customizable Data Grids with `react-table`
Learn how to build highly customizable and performant data tables in your React applications using the `react-table` library. This tutorial demonstrates `react-table`'s features, including pagination, sorting, and filtering, providing a practical guide to displaying and managing tabular data.
Using react-table in React
This guide demonstrates how to use the `react-table` library to display data in a customizable table within your React application.
Introduction to react-table
react-table
is a lightweight, performant, and highly customizable data grid component for React. It offers features like pagination, filtering, and sorting, all while remaining highly flexible.
Features
- Lightweight (~11kb)
- Fully customizable (JSX, templates, styling, callbacks)
- Client-side and server-side pagination
- Filtering capabilities
- Pivoting and aggregation
- Easily themeable
Installation
- Create a new React app:
npx create-react-app my-react-app
- Install
react-table
:npm install react-table
Implementing react-table
Let's create a simple table to display some sample data.
Sample Data and Columns
Data and Columns Definition
const data = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 35 },
{ name: 'Dave', age: 28 }
];
const columns = [
{ Header: 'Name', accessor: 'name' },
{ Header: 'Age', accessor: 'age' }
];
App.js
App.js
import React from 'react';
import ReactTable from "react-table";
import "react-table/react-table.css";
function App() {
const data = [ /* ...data from above */ ];
const columns = [ /* ...columns from above */ ];
return (
<ReactTable data={data} columns={columns} />
);
}
export default App;
Output (Example)
This section would show an image of the rendered table. The table would have columns 'Name' and 'Age' and rows containing the data defined above. Example:
Name | Age |
---|---|
Alice | 30 |
Bob | 25 |
Charlie | 35 |
Dave | 28 |
(Another image here showing the table with a different number of rows selected via a dropdown, demonstrating pagination).