In this tutorial, I’ll teach you how to use the react-data-table-component library in your React projects. You’ll learn how to render a table that has features such as pagination, searching/filtering, and sorting.
I’ll walk you through each step, from setting up a React and TypeScript project with Vite to using the react-data-table-component library to render a table.
To follow along with this tutorial, there are a few prerequisites:
- A basic understanding of React and TypeScript
- Basic knowledge of Bootstrap, which we’ll use for styling
- For Windows OS users, know how to use the PowerShell terminal (as you’ll need an interactive terminal to create the project with Vite)
- Node v20.11.1 installed
- npm v10.2.4 installed
- A code editor such as Visual Studio Code (VS Code) or Atom
Let’s start creating the project!
What will we build
By the end of this tutorial, you will have built a table that displays a person’s ID, name, height, and eye color.
The table will also have a search bar where users can search for a person based on the value of any of the four properties mentioned above.
Each row in the table will be selectable and each column will be sortable when the column header is clicked by a user.
1. Create a React and TypeScript Project
In the command line, create the project with the command below:
npm create vite@latest
Name the project react-data-table-tutorial
.
Navigate to React with the up and down keys to select React. Choose React as the library you’re going to use.
Similar to the step above, navigate to TypeScript and choose it as the language to use.
The next step is to change directories into the project’s folder. Once you’ve done that, open the project in your code editor as seen in Figure 1.2 below. I’ll be using the VS Code editor in this tutorial.
2. Remove the Default Code Set by React
This step is just to remove any default code and stylings in the files found in the src folder.
First, remove all default styling in the App.css file and replace it with the stylings below.
Then remove the following from the App.tsx file:
- the imported statements of the
useState
hook,reactLogo
, and theviteLogo
. - The destructured array and
useState
hook for the count variable andsetCount
function. - The returned JSX wrapped within the fragment of the
App
component.
The App
component should look like the code-block below after all of those changes:
3. Install the Libraries We’ll Need
In the project, you will need to install a few libraries:
- styled-components v3.23+
- react-data-table-component v16.8.0+
- Bootstrap v5.3.3
You will need to install the styled-components library to use with the react-data-table-component library.
In the command line, install all of these by using the commands below:
npm install styled-components
npm install react-data-table-components
npm install [email protected]
The installed libraries are listed under dependencies in the package.json file.
4. Import Bootstrap in the App Component
In the App.tsx file, import the Bootstrap library at the top of the file.
This will allow us to use Bootstrap stylings throughout the project.
5. Create a Table Component
In the src folder, create a sub-directory and name it components. By convention, this is to hold all of the components of the project.
In the components folder, create a file called Table.tsx. This is for our Table
component.
Create a function component called Table
.
6. Import the react-data-table-component Library to Start Using it
Import the react-data-table-component library into the Table
component.
7. Create the Table in the Table Component
Create a <div>
container within the fragment and add Bootstrap styling of container
and my-5
to place the table in the center of the page.
Add the DataTable
component as the child of the <div>
container.
Pass the columns and rows of the table as props to the DataTable
component.
The column headers of the table are objects that will be stored in an array. The rows of the table will also be stored in a similar way. The array of objects for both will have these structures:
These constants are then passed into the DataTable
component as such:
Create a columns
constant that is an array of objects that has four columns headers for personID
, fullName
, height
, and eyeColor
.
Create a rows
constant that is an array of objects that has 15 objects which is equivalent to data for 15 people.
Pass the two constants into their respective props in the DataTable
component as seen in the next code-block.
You will also add the fixedHeader
prop to the DataTable
component to keep the column header fixed when a user scrolls down the table that has more than 10 records.
Give the table a title by passing the title
prop to the component and its value is whatever you would like to call your table.
Back in the App
component, import the Table
component and place it within the fragment.
8. Add Pagination and Sorting, and Make Each Row Selectable
Pagination and selectableRows
Add the pagination
and selectableRows
props to the DataTable
component.
By default, the first page has the first 10 records. If there needs to be API calls from the server side for custom pagination, then you can use the paginationServer
property along with the paginationTotalRows
, onChangeRowsPerPage
and onChangePage
properties which work in conjunction with a few other things.
But for now, let’s stick with the pagination
property.
Sorting
In the DataTable
component, add the sortable
property to each object in the columns
constant. Give it a boolean value of true
so that sorting is applied to each column when a user clicks on the column headers.
9. Add Searching and Filtering
Add a <div>
container above the DataTable
component in the returned JSX. Also add the bootstrap input-group
class to the <div>
.
The child element of this new container will be the <input />
of type search
and we’ll use Bootstraps’ stylings for it. Use the code below:
An optional step is to add a search icon or use Bootstraps’ default styling for a search bar. However, we’ll leave it out for now so we can focus solely on the searching functionality.
Now, let’s import the useState
hook into the Table.tsx file.
Use the hook and pass in the rows
constant as the default value of our state variable. Within the destructured array will be our state variable called data
and the setter function called setData
.
Create a function called handleSearch
that will be called when the onChange
event listener is used on the search bar.
Pass in the event object, e
, as its argument. Using type annotation, set the type of the event object to React.ChangeEvent<HTMLInputElement>
.
Declare 5 variables of Boolean type as seen below:
const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
let searchValue: Boolean;
let personIDValue: Boolean;
let fullNameValue: Boolean;
let heightValue: Boolean;
let eyeColorValue: Boolean;
};
Declare a new local constant called newRows
. In this constant, filter out and return the rows/data in the rows constant where the search input’s value is the same as the value of the rows’ fullName
or height
or eyeColor
property.
Pass newRows
into the setter function, setData
.
Next step is to pass (not call the function) the handleSearch
function into the onChange
event listener.
The value of the data property in the DataTable
component will now be the state variable, data
instead of rows
that was initially passed in.
10. View the Table
Run the project in the command line and view the table in a browser:
The table should look something like this:
You can read through the documentation for react-data-table-component as it has more in-depth coverage of the library’s usage that goes beyond the scope of this article.
Don’t fret if there’s a difference in your table from the expected result. The source code in this repository will guide you.
Happy coding and may your code run smoothly!