How to create a table component in React

Allie Beazell
Allie Beazell

Jul 27, 2020

If you’re building internal tools—admin panels, dashboards, CRMs, you name it—chances are you’re thinking about how to build a table component to display, edit, and manipulate data. And if you’re working in React, you (thankfully) don’t need to build it from scratch: the react-table library gives you Hooks to get tables up and running quickly.

By the end of this tutorial, you’ll know how to:

  • Build a simple React table and modify data, columns, and headers
  • Give your React table a custom UI by passing CSS into each component using styled-components or piggybacking off a React component library.
  • Extend your React table with more features like sorting, filtering, and pagination.

Learning all of these things yourself can be complicated. Thankfully, the react-table library is killer (and we made you this guide to help).

What is react-table?

React-table is an open-source library specifically for building (you guessed it) tables in React. The library has over 11.5k stars on GitHub and is used by tons of big-name tech companies, like Google, Apple, and Microsoft. Plus, we like it so much here at Retool that we sponsor it.

We like react-table because it’s easy to set up, customize, and extend. The library covers the basics of a useful table — sorting, filtering, and pagination — but also goes much deeper with advanced features like:

  • Grouping
  • Expanded State
  • Custom Plugin Hooks

It should be noted that react-table is a “headless” UI library. The library doesn’t actually render a user interface. While this may sound strange, it was designed this way to give you more control over the look and feel of the react-table component while keeping the package size small. Don’t worry, adding UI is easy, and we will cover that later on.

Since we’re all about internal tools, let’s imagine that we are building a table to display order information for customer service reps. Our table will need to display customer info (name and address) and order info (order number and date) for each customer’s purchase.

When you’ve finished working through this tutorial, you will have five versions of a table built with react-table: simple, styled, sortable, filterable, and paged. Below is the final, paged version we’re aiming for.

It could look better, but that’s what CSS is for!

Build a simple table with react-table

First, we’ll build a basic table to display data, no styling or extra features. Our customer support reps need an easy way to view order information for each customer. Our simple table will have two top-level headers: User Info and Order Info. Under User Info, we need two secondary headers to display each customer’s Name and Address. Under Order Info, we need two more secondary headers to display the Date that the order was made and the Order Number.

In this section, we’ll build a table with four columns split into two groups. We’ll break down how to define the shape of column objects and data, parse header groups, and fill out our rows and cells. At the end, expect to see something like this:

Note that we did add a tiny bit of extra styling to this section, so the table has lines.

Taking care of housekeeping basics first, you’ll need to install react-table by using a package manager (Yarn or npm) and import the library into your React app:

1import { useTable } from 'react-table';

Then, once react-table is installed and imported, it’s time to define our data and columns by way of the useTable Hook. React-table leverages Hooks, which are a fairly new addition to React (as of version 16.8). If you’re not familiar with React Hooks, we recommend taking a look at React’s Hooks at a Glance documentation.

The most important Hook for our table is useTable. We’ll pass two arguments to useTable:

  • data = table data defined with the useMemo Hook (data must be memo-ized before it can be passed to useTable to cut down on calculation time by preventing unchanged data from being rerun)
1const data = React.useMemo(() =>
2 [
3 {
4 name: 'Kim Parrish',
5 address: '4420 Valley Street, Garnerville, NY 10923',
6 date: '07/11/2020',
7 order: '87349585892118',
8 },
9 {
10 name: 'Michele Castillo',
11 address: '637 Kyle Street, Fullerton, NE 68638',
12 date: '07/11/2020',
13 order: '58418278790810',
14 },
15 {
16 name: 'Eric Ferris',
17 address: '906 Hart Country Lane, Toccoa, GA 30577',
18 date: '07/10/2020',
19 order: '81534454080477',
20 },
21 {
22 name: 'Gloria Noble',
23 address: '2403 Edgewood Avenue, Fresno, CA 93721',
24 date: '07/09/2020',
25 order: '20452221703743',
26 },
27 {
28 name: 'Darren Daniels',
29 address: '882 Hide A Way Road, Anaktuvuk Pass, AK 99721',
30 date: '07/07/2020',
31 order: '22906126785176',
32 },
33 {
34 name: 'Ted McDonald',
35 address: '796 Bryan Avenue, Minneapolis, MN 55406',
36 date: '07/07/2020',
37 order: '87574505851064',
38 },
39 ],
40 []
  • columns = column definitions defined with the useMemo Hook (column defs must be memoized before they can be passed to useTable)
1const columns = React.useMemo(
2 () => [
3 {
4 Header: 'User Info',
5 columns: [
6 {
7 Header: 'Name',
8 accessor: 'name',
9 },
10 {
11 Header: 'Address',
12 accessor: 'address',
13 },
14 ],
15 },
16 {
17 Header: 'Order Info',
18 columns: [
19 {
20 Header: 'Date',
21 accessor: 'date',
22 },
23 {
24 Header: 'Order #',
25 accessor: 'order',
26 },
27 ],
28 },
29 ],
30 []

Take a second to look at the relationship between data and columns. The accessor in columns is the “key” in the data object. This is important to be able to access the right data for each column once we use useTable.

Once we have defined data and columns, it’s time to implement our useTable Hook. Pass data and columns into useTable, which will return properties that we can extract to build our table UI.

1const {
2 getTableProps,
3 getTableBodyProps,
4 headerGroups,
5 rows,
6 prepareRow,
7} = useTable({ columns, data })

Now, we’ll use these extracted properties to build out our table via familiar JSX tags – <table>, <thead>, <tr>, <th>, and <tbody> – and then fill in our properties from useTable.


First, we need <table> to wrap the rest of our code, and we need to pass the getTableProps() function in to resolve any table props.

1<table {...getTableProps()}>
2 ...


Things start to heat up a little bit when we start to build our headers! On a high level, all we are doing is creating our header rows using the column header names that we defined above. Before we jump into the code, let’s look at the rendered table to get a better idea:

Each circled section in the table above is a headerGroup, which is simply an object that contains an array of headers for that row. For this table, we will have two header rows: the header circled in red is the first headerGroup, and the header circled in blue is the second headerGroup.

To get the data we need to build these headers out of headerGroups, we will be using JavaScript’s map() method. If you’re unfamiliar with it, take a second to read the docs.

First, we have our <thead> tag, which is simple enough. Inside of that tag, we are going to use map() to parse each headerGroup, creating a new row using the <tr> and passing that headerGroup’s getHeaderGroupProps() method in.

1{ => (
2   <tr {...headerGroup.getHeaderGroupProps()}>
3     ...
4   </tr>

Inside of the <tr>, we use map() again, but this time on the array of headers. Each header object has a Header property (which is the name you’ll give each header), a render() function, and another prop resolver function called getHeaderProps().

For each column, we use the <th> tag to create the column, being sure to pass that column’s prop resolver function getHeaderProps() and then use the render() function to access the Header.

2 { => (
3   <tr {...headerGroup.getHeaderGroupProps()}>
4     { => (
5       <th {...column.getHeaderProps()}>{column.render('Header')}</th>
6     ))}
7   </tr>
8 ))}

Table Body

Similar to how we did <table> and <thead>, we add <tbody> and pass the prop resolver function getTableBodyProps() in. Then, we use map() to iterate through rows, which is an array of Row objects. Each Row object has a cells field, which is just an array of Cell objects that contain the data for each cell in the row.

The orange circle shows a row, and the pink circle shows a cell.

For each row, we need to pass the row object to the prepareRow() function, which helps with rendering efficiently. Next, we return <tr> tags to render the row. In each <tr>, we again use map() to parse cells. For each cell, we create a <td> tag, pass in the prop resolver function getCellProps(), and then render the cell data using the render() method.

1<tbody {...getTableBodyProps()}>
2 { => {
3   prepareRow(row)
4   return (
5     <tr {...row.getRowProps()}>
6       { => {
7         return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
8       })}
9     </tr>
10   )
11 })}

Let’s put all of that together to render our table.

1return (
2 <table {...getTableProps()}>
3   <thead>
4     { => (
5       <tr {...headerGroup.getHeaderGroupProps()}>
6         { => (
7           <th {...column.getHeaderProps()}>{column.render('Header')}</th>
8         ))}
9       </tr>
10     ))}
11   </thead>
12   <tbody {...getTableBodyProps()}>
13     { => {
14       prepareRow(row)
15       return (
16         <tr {...row.getRowProps()}>
17           { => {
18             return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
19           })}
20         </tr>
21       )
22     })}
23   </tbody>
24 </table>

Using the above code, you’ll end up with a rendered table that looks like this:

Give your table a custom UI

Even if you’re building a tool that will only be used by an internal team, it’s still important for the UI to look good (or at least not terrible). Styling (at least the basics) is extra important with react-table too since no components are actually rendered as part of the library. Without any styling, you’ll end up with a table like this:

You can style your react-table component by either creating custom styles or through a React component library. The final product from this section will look like this:

Isn’t it beautiful?

Using the style Prop

Styling your table with react-table is as simple as passing CSS into the style prop of each component. Let’s look at the <th> tag for styling a header row:

2 {...column.getHeaderProps()}
3 style={{
4   borderBottom: 'solid 3px blue',
5   background: 'green',
6   color: 'white',
7   fontWeight: 'bold',
8 }}

You can also use CSS files and CSS modules if you’d like. Check out React’s CSS docs for more info.

Using styled-components

styled-components is a nifty React library that lets you style React components with CSS directly inside of JS code (as opposed to external CSS files). Lately, it’s become a really popular way to handle component styling in React, so you might want to use it for your table.

To use styled-components, install the library and import it into your project. Create a component Styles that uses styled from the styled-components library to create a div with the styles for your table in CSS. Move all of your code for creating the Table component into its own function. Then, in your App function (where your columns and data are defined), return <Styles> with your <Table> rendered inside. This will apply the styles from the styled-components on to your table.

1import styled from 'styled-components'
3/* Pssst this has the rest of the styling we've been using
4to give our table borders and non-ugly spacing */
6const Styles = styled.div`
7 table {
8   border-spacing: 0;
9   border: 1px solid black;
11   tr {
12     :last-child {
13       td {
14         border-bottom: 0;
15       }
16     }
17   }
19   th,
20   td {
21     padding: 0.5rem;
22     border-bottom: 1px solid black;
23     border-right: 1px solid black;
25     :last-child {
26       border-right: 0;
27     }
28   }
30   th {
31     background: green;
32     border-bottom: 3px solid blue;
33     color: white;
34     fontWeight: bold;
35   }
36 }
39function Table({ columns, data }) {
40 const {
41   getTableProps,
42   getTableBodyProps,
43   headerGroups,
44   rows,
45   prepareRow,
46 } = useTable({
47   columns,
48   data,
49 })
51 // Render the UI for your table
52 return (
53   <table {...getTableProps()} >
54     ...
55   </table>
56 )
59function App() {
60 const columns = React.useMemo(...)
62 const data = React.useMemo(...)
64 return (
65   <Styles>
66     <Table columns={columns} data={data} />
67   </Styles>
68 )
71export default App

Using a React Component Library

If you don’t want to style things yourself, using a React component library is the way to go. For this example, we’re going to use the material-ui library to create a nice table with react-table.

Going off of the Cell styling example above, we simply have to import TableCell from @material-ui/core/TableCell.

1import TableCell from '@material-ui/core/TableCell'
3<TableCell {...cell.getCellProps()}>
4   {cell.render('Cell')}

This will bring all of the styling of the TableCell component in material-ui. No extra work for you!

Extend your table with more features

No table worth rendering is going to have only two columns and three rows, like our example. Most likely, you're going to have a hefty portion of columns and row upon row of data. You'll need features to allow users to sift through all that data, like sorting, filtering, and pagination.


We want to give our customer service reps the ability to easily find what they are looking for, and sorting is a great way to accomplish that! If the reps want to see the most recent orders placed, they can sort by date from the Date column. If they want to scan through the customers alphabetically, they can sort by name in the Name column.

Sorting is accomplished by using the useSortBy Hook from react-table. Be sure to add that to your import statements:

1import { useTable, useSortBy } from 'react-table' 

Next, you’ll need to pass useSortBy into the useTable Hook arguments:

1const {
2 getTableProps,
3 headerGroups,
4 rows,
5 getRowProps,
6 prepareRow,
7} = useTable(
8 {
9   columns,
10   data,
11 },
12 useSortBy,

If you want the sorting to be done by anything other than the default alphanumeric value, you’ll need to update your columns definition with a sortType field. Sorting options include:

  • alphanumeric = Best for sorting letters and numbers (default)
  • basic = Best for sorting numbers between 0 and 1
  • datetime = Best for sorting by date

For this example, we will be using the default, but if you needed to add that code, it would look like this:

1const columns = React.useMemo(
2 () => [
3   {
4     Header: 'Rating',
5     accessor: 'rating',
6     sortType: 'basic',
7   },
8 ],
9 []

Now there are two more things to do. First, pass the getSortByToggleProps() function into your getHeaderProps() function. The getSortByToggleProps() function resolves props for toggling the sort direction when the user clicks on the header.

Second, add a span tag to display an arrow up, an arrow down, or nothing to the column header to indicate how that column is sorted. You can determine how the column is sorted by checking column properties isSorted and isSortedDesc.

2 { => (
3   <tr {...headerGroup.getHeaderGroupProps()}>
4     { => (
5       <th {...column.getHeaderProps(column.getSortByToggleProps())}>
6         {column.render('Header')}
7         <span>
8           {column.isSorted ? (column.isSortedDesc ? ' 🔽' : ' 🔼') : ''}
9         </span>
10       </th>
11     ))}
12   </tr>
13 ))}

Check out this code sandbox for a more advanced version of sorting using react-table.


For the sake of simplicity, this tutorial is going to focus on how to add a text filter on the columns of our simple table. This will give our customer support reps the ability to quickly find the information they are looking for. If a customer contacts them, the rep can easily search the Names column for that customer to find their orders or search the Order Numbers column to look up a specific order.

For further examples of all the different kinds of filters (including global, which is really useful), check out this the react-table docs.

Filtering is accomplished by using the useFilters() Hook from react-table. Be sure to add that to your import statements:

1import { useTable, useFilters } from 'react-table' 

Next, you’ll need to pass useFilters into the useTable Hook arguments:

1const {
2 getTableProps,
3 headerGroups,
4 rows,
5 getRowProps,
6 prepareRow,
7} = useTable(
8 {
9   columns,
10   data,
11 },
12 useFilters,

Now, we are going to add the UI for the column filter into our table. If there is a filter applied to this column, we render the UI for the filter by calling the column’s render() method on the Filter field. Otherwise, do nothing.

1<th {...column.getHeaderProps()}>
2 {column.render('Header')}
3 <div>{column.canFilter ? column.render('Filter') : null}</div>

But wait! We haven’t defined the UI for the filter yet. We’ll need a function to do that – for our filter function, we first want to find out how many rows are left to be filtered, so we can display that number to the user as an input placeholder. Then, we will render an <input> for the user to type what they want to filter.

1function TextFilter({
2 column: { filterValue, preFilteredRows, setFilter },
3}) {
4 const count = preFilteredRows.length
6 return (
7   <input
8     value={filterValue || ''}
9     onChange={e => {
10       setFilter( || undefined)
11     }}
12     placeholder={`Search ${count} records...`}
13   />
14 )

Our TextFilter() function receives three values from column:

  • filterValue = the current value that this column is using to filter.
    • This value is set from the table’s state filters object.
  • preFilteredRows = Array of rows passed to the column before any filtering was done.
  • setFilter = function that takes in a filterValue in order to update the filterValue of this column (in this case, it’s taking the value that the user types into the <input>).

Once we’ve defined our filter function, we’ll update the definition of our column object to have a Filter field. Add this code to your Table function:

1const defaultColumn = React.useMemo(
2 () => ({
3   Filter: TextFilter,
4 }),
5 []

Finally, make sure to pass defaultColumn in with the columns and data arguments when you use useTable():

1const {
2 getTableProps,
3 ...
4} = useTable(
5 {
6   columns,
7   data,
8   defaultColumn,
9 },
10 useFilters,

The data we created for the example in this tutorial is pretty small compared to what you would see in the real world. Only six rows? Please. In reality, our customer support reps would be dealing with hundreds (maybe even thousands) of rows of customer data. To avoid long render times and ginormous pages to scroll through, we are going to add pagination to our table. This will allow react-table to only deal with rendering some rows at a time and will take some strain off the customer support reps from having to look at overwhelming amounts of data.

Pagination is accomplished by using the usePagination() Hook from react-table. Be sure to add that to your import statements:

1import { useTable, usePagination } from 'react-table' 

Next, you’ll need to pass usePagination() into the useTable() Hook arguments, set the initial state (if you want to start the pageIndex at anything other than 0 or have the pageSize bigger or smaller than 10), and extract extra properties from what it returns.

1const {
2   getTableProps,
3   headerGroups,
4   getRowProps,
5   prepareRow,
6   page,
7   pageOptions,
8   state: { pageIndex, pageSize },
9   previousPage,
10   nextPage,
11   canPreviousPage,
12   canNextPage,
13 } = useTable(
14   {
15     columns,
16     data,
17     initialState: { pageSize: 2 },
18   },
19   usePagination,
20 )

Note that as well as iterating through rows in <tbody> as we did previously before pagination, we are going to iterate through page, which is similar to rows except it only has the number of rows that fit on the page. If you don’t do this, you can click those buttons as much as you want — the data won’t move. Trust me.

1<tbody {...getTableBodyProps()}>
2 { => {
3   prepareRow(row)
4   ...

In this example, we have a button to go to the Previous Page, a button to go to the Next Page, and an input that lets the user type a page number to jump to.

1return (
2   <div>
3     <table {...getTableProps()}>
4       ...
5     </table>
6     <div>
7       <button onClick={() => previousPage()} disabled={!canPreviousPage}>
8         Previous Page
9       </button>
10       <button onClick={() => nextPage()} disabled={!canNextPage}>
11         Next Page
12       </button>
13       <div>
14         Page{' '}
15         <em>
16           {pageIndex + 1} of {pageOptions.length}
17         </em>
18       </div>
19     </div>
20   </div>
21 )

Check out this code sandbox for a more advanced version of pagination using react-table.

Your table component with react-table

Hopefully, this tutorial helps you understand how to create, style, and extend a table in React using react-table. For more advanced tutorials, we recommend checking out the “Kitchen Sink” example from react-table’s docs. It has fully fleshed-out examples of most things that react-table has to offer, from pagination and sorting to filtering, grouping, expandable rows, and row selection.

Retool provides you with pre-built React components like the React table for quickly building apps right out of the box. Check out the Retool’s Component library or Docs for more info, or start building for free on Retool today.


Allie Beazell
Allie Beazell
Jul 27, 2020