How to implement DataTable in React
Today we will show you how to implement DataTable in React. There are many tables available in ReactJS but the datatable is a table component that is available with advanced options.
In this article, we will create a demo that contains a simple datatable with sorting and pagination functionality.
Steps to implement DataTable in React
- Create react application
- Install datatable package
- Integrate datatable with sorting and pagination
- Output
1. Create react application
Let’s create a simple react app using `create-react-app`. Run the following command to create an application.
npx create-react-app react-datatable-example
2. Install datatable package
To integrate datatable, we will have to install the react-data-table-component npm package in the application.
Here we have to install the `styled-components` along with the `react-data-table-component` package. Execute the following command to install datatable.
npm install react-data-table-component styled-components
3. Integrate datatable with sorting and pagination
Let’s split the datatable integration into the below points.
- Use the sample data JSON to show the data into the table.
- Create a list of columns by using `name`, `selector` and `sortable` attributes. Visit columns props for more advanced features.
- Import the `react-data-table-component` package in the react component.
- Render the `DataTable` component by passing the columns, data, pagination and other attributes.
Check out the following code to integrate DataTable.
data.json
[
{
"name": "Katelyn T. Boyle",
"phone": "1-389-886-8523",
"email": "[email protected]",
"dob": "07/11/1997"
},
{
"name": "September Y. Forbes",
"phone": "879-4512",
"email": "[email protected]",
"dob": "01/11/1968"
},
{
"name": "Kaseem T. Potts",
"phone": "612-9561",
"email": "[email protected]",
"dob": "11/04/1995"
},
{
"name": "Maite Mcintosh",
"phone": "1-727-227-3534",
"email": "[email protected]",
"dob": "30/08/1988"
},
{
"name": "Kerry Calderon",
"phone": "1-730-492-6543",
"email": "[email protected]",
"dob": "07/11/1973"
},
{
"name": "Hakeem W. Fowler",
"phone": "897-7252",
"email": "[email protected]",
"dob": "10/06/1987"
},
{
"name": "Steel Bruce",
"phone": "1-233-437-3487",
"email": "[email protected]",
"dob": "06/08/1978"
},
{
"name": "Joseph Henderson",
"phone": "1-881-206-0743",
"email": "[email protected]",
"dob": "14/06/1988"
},
{
"name": "Quinn L. Lamb",
"phone": "999-8845",
"email": "[email protected]",
"dob": "24/08/1998"
},
{
"name": "Hadley Shields",
"phone": "1-690-958-1438",
"email": "[email protected]",
"dob": "11/09/1985"
},
{
"name": "Tara S. Clarke",
"phone": "539-3379",
"email": "[email protected]",
"dob": "18/07/1974"
},
{
"name": "Travis Zamora",
"phone": "1-410-239-6938",
"email": "[email protected]",
"dob": "16/03/1980"
},
{
"name": "Hilel Chandler",
"phone": "1-842-693-3527",
"email": "[email protected]",
"dob": "01/07/1983"
},
{
"name": "Maisie Phelps",
"phone": "1-695-407-3574",
"email": "[email protected]",
"dob": "13/10/1980"
},
{
"name": "Heidi Montgomery",
"phone": "1-264-258-4447",
"email": "[email protected]",
"dob": "10/12/1971"
},
{
"name": "Jackson Oneil",
"phone": "1-638-155-3149",
"email": "[email protected]",
"dob": "13/08/1983"
},
{
"name": "Savannah Q. Osborn",
"phone": "392-6188",
"email": "[email protected]",
"dob": "18/06/1999"
},
{
"name": "Lesley C. Le",
"phone": "828-1474",
"email": "[email protected]",
"dob": "30/11/1978"
},
{
"name": "Brenden R. Vaughn",
"phone": "1-651-993-5457",
"email": "[email protected]",
"dob": "09/08/1984"
},
{
"name": "Beverly V. Mosley",
"phone": "1-880-511-8690",
"email": "[email protected]",
"dob": "13/10/1975"
},
{
"name": "Aquila H. Riddle",
"phone": "1-519-622-5158",
"email": "[email protected]",
"dob": "18/12/1990"
},
{
"name": "Daryl V. Strong",
"phone": "891-6228",
"email": "[email protected]",
"dob": "26/06/2000"
},
{
"name": "Garth Hooper",
"phone": "1-974-801-0172",
"email": "[email protected]",
"dob": "10/05/1981"
},
{
"name": "Lev Fisher",
"phone": "1-774-490-6618",
"email": "[email protected]",
"dob": "17/11/1975"
},
{
"name": "Jaden Caldwell",
"phone": "1-893-290-7035",
"email": "[email protected]",
"dob": "01/11/1991"
},
{
"name": "Courtney B. Leon",
"phone": "747-1596",
"email": "[email protected]",
"dob": "01/11/1985"
},
{
"name": "Meredith Z. Martinez",
"phone": "1-523-283-9358",
"email": "[email protected]",
"dob": "04/07/1988"
},
{
"name": "Pamela G. Roach",
"phone": "1-956-188-5509",
"email": "[email protected]",
"dob": "10/01/1983"
},
{
"name": "Nolan Kirk",
"phone": "580-5661",
"email": "[email protected]",
"dob": "31/05/1977"
},
{
"name": "Damon Stokes",
"phone": "376-6414",
"email": "[email protected]",
"dob": "21/02/1997"
},
{
"name": "Ila Daugherty",
"phone": "207-9279",
"email": "[email protected]",
"dob": "11/08/1973"
},
{
"name": "Florence M. Booth",
"phone": "1-679-613-5539",
"email": "[email protected]",
"dob": "30/08/1969"
},
{
"name": "Dorian Luna",
"phone": "1-711-593-5673",
"email": "[email protected]",
"dob": "29/06/1976"
},
{
"name": "Ira Crawford",
"phone": "390-0417",
"email": "[email protected]",
"dob": "14/05/1992"
},
{
"name": "Christopher I. Davenport",
"phone": "513-0131",
"email": "[email protected]",
"dob": "04/01/1986"
},
{
"name": "Kiona Pollard",
"phone": "468-8632",
"email": "[email protected]",
"dob": "16/11/1976"
},
{
"name": "Blossom Barry",
"phone": "164-3542",
"email": "[email protected]",
"dob": "24/05/1995"
},
{
"name": "Janna Snider",
"phone": "1-879-238-4633",
"email": "[email protected]",
"dob": "20/04/1988"
},
{
"name": "Larissa Hampton",
"phone": "1-766-588-2166",
"email": "[email protected]",
"dob": "07/12/1988"
},
{
"name": "Declan O. Kim",
"phone": "752-0415",
"email": "[email protected]",
"dob": "24/10/1986"
},
{
"name": "Philip Owen",
"phone": "246-6791",
"email": "[email protected]",
"dob": "25/12/1986"
},
{
"name": "Vance Ratliff",
"phone": "440-5801",
"email": "[email protected]",
"dob": "14/12/1979"
},
{
"name": "Dorothy E. Garrison",
"phone": "1-619-930-8150",
"email": "[email protected]",
"dob": "04/11/1974"
},
{
"name": "Tatyana T. Day",
"phone": "1-391-764-1647",
"email": "[email protected]",
"dob": "03/06/1982"
},
{
"name": "Drew Kennedy",
"phone": "1-577-704-7362",
"email": "[email protected]",
"dob": "08/02/1968"
},
{
"name": "Coby Sanford",
"phone": "793-9294",
"email": "[email protected]",
"dob": "03/01/1991"
},
{
"name": "Hayfa W. Melton",
"phone": "562-7157",
"email": "[email protected]",
"dob": "21/03/1988"
},
{
"name": "Uta Stuart",
"phone": "210-1895",
"email": "[email protected]",
"dob": "04/03/1982"
},
{
"name": "Jemima Houston",
"phone": "1-349-409-5624",
"email": "[email protected]",
"dob": "06/11/1968"
},
{
"name": "Cailin M. Sanford",
"phone": "220-9543",
"email": "[email protected]",
"dob": "03/02/1971"
},
{
"name": "Kibo H. Atkinson",
"phone": "468-6253",
"email": "[email protected]",
"dob": "23/11/1974"
},
{
"name": "Mason Q. Hopper",
"phone": "1-416-117-9588",
"email": "[email protected]",
"dob": "31/01/1971"
},
{
"name": "Rogan White",
"phone": "318-1198",
"email": "[email protected]",
"dob": "27/06/1985"
},
{
"name": "Kibo Petty",
"phone": "1-795-166-9081",
"email": "[email protected]",
"dob": "08/03/1982"
},
{
"name": "Wynne Wolf",
"phone": "664-1755",
"email": "[email protected]",
"dob": "14/06/1985"
},
{
"name": "Piper Mccormick",
"phone": "1-816-664-9420",
"email": "[email protected]",
"dob": "22/05/1987"
},
{
"name": "Tanek Browning",
"phone": "885-2917",
"email": "[email protected]",
"dob": "29/10/1983"
},
{
"name": "Linus T. Harding",
"phone": "151-2485",
"email": "[email protected]",
"dob": "03/06/1971"
},
{
"name": "Desirae Z. Brock",
"phone": "1-806-971-4667",
"email": "[email protected]",
"dob": "04/09/1995"
},
{
"name": "Donna Cunningham",
"phone": "1-787-837-4123",
"email": "[email protected]",
"dob": "02/04/1994"
},
{
"name": "Octavius J. Navarro",
"phone": "301-5545",
"email": "[email protected]",
"dob": "23/07/1979"
},
{
"name": "Jane Mann",
"phone": "692-0585",
"email": "[email protected]",
"dob": "06/05/1992"
},
{
"name": "Brielle Wallace",
"phone": "1-942-665-9007",
"email": "[email protected]",
"dob": "30/08/1974"
},
{
"name": "Malcolm Velez",
"phone": "1-638-145-7361",
"email": "[email protected]",
"dob": "23/03/1985"
},
{
"name": "Karina Ochoa",
"phone": "1-374-187-8585",
"email": "[email protected]",
"dob": "19/01/1987"
},
{
"name": "Cherokee N. Paul",
"phone": "1-897-458-7214",
"email": "[email protected]",
"dob": "29/10/1979"
},
{
"name": "Wang Spears",
"phone": "1-745-264-0768",
"email": "[email protected]",
"dob": "28/11/1986"
},
{
"name": "Leila F. Pittman",
"phone": "980-6400",
"email": "[email protected]",
"dob": "11/06/1999"
},
{
"name": "Bevis Benton",
"phone": "1-670-320-1073",
"email": "[email protected]",
"dob": "18/10/1991"
},
{
"name": "Vivian N. Quinn",
"phone": "1-945-440-8685",
"email": "[email protected]",
"dob": "08/02/1983"
},
{
"name": "Delilah X. Henderson",
"phone": "410-5213",
"email": "[email protected]",
"dob": "07/09/1972"
},
{
"name": "Harrison Alston",
"phone": "814-6328",
"email": "[email protected]",
"dob": "20/02/1971"
},
{
"name": "Rhea B. Lambert",
"phone": "1-185-364-7519",
"email": "[email protected]",
"dob": "16/09/1993"
},
{
"name": "Dennis M. Munoz",
"phone": "198-7742",
"email": "[email protected]",
"dob": "10/08/1995"
},
{
"name": "Caldwell Shelton",
"phone": "410-3089",
"email": "[email protected]",
"dob": "18/09/1975"
},
{
"name": "Amir Juarez",
"phone": "1-999-456-1203",
"email": "[email protected]",
"dob": "10/02/1975"
},
{
"name": "Fredericka Fry",
"phone": "251-2786",
"email": "[email protected]",
"dob": "08/07/1979"
},
{
"name": "Jesse G. Nichols",
"phone": "574-8398",
"email": "[email protected]",
"dob": "24/06/1973"
},
{
"name": "Shana Duncan",
"phone": "739-2387",
"email": "[email protected]",
"dob": "20/08/1993"
},
{
"name": "Shafira Morris",
"phone": "1-758-466-1432",
"email": "[email protected]",
"dob": "13/03/1976"
},
{
"name": "Indigo Cruz",
"phone": "1-301-287-4515",
"email": "[email protected]",
"dob": "09/01/1982"
},
{
"name": "Hilda Nguyen",
"phone": "507-8349",
"email": "[email protected]",
"dob": "13/04/1979"
},
{
"name": "Tucker D. Guy",
"phone": "1-963-823-0346",
"email": "[email protected]",
"dob": "22/01/1983"
},
{
"name": "Guinevere I. Parsons",
"phone": "1-215-694-7661",
"email": "[email protected]",
"dob": "01/04/1985"
},
{
"name": "Roanna Serrano",
"phone": "855-8131",
"email": "[email protected]",
"dob": "03/07/1991"
},
{
"name": "Velma I. Cobb",
"phone": "1-979-311-2517",
"email": "[email protected]",
"dob": "11/11/1967"
},
{
"name": "Amery G. Ayala",
"phone": "509-2912",
"email": "[email protected]",
"dob": "25/02/1976"
},
{
"name": "Josiah E. Rios",
"phone": "1-221-800-5552",
"email": "[email protected]",
"dob": "20/08/1973"
},
{
"name": "Malachi Q. Tran",
"phone": "193-9929",
"email": "[email protected]",
"dob": "02/10/1987"
},
{
"name": "Erica Perez",
"phone": "1-381-238-7988",
"email": "[email protected]",
"dob": "21/08/1980"
},
{
"name": "Quinn A. Spence",
"phone": "1-465-928-7340",
"email": "[email protected]",
"dob": "07/01/1992"
},
{
"name": "Jolene Delacruz",
"phone": "1-285-575-9532",
"email": "[email protected]",
"dob": "10/08/1990"
},
{
"name": "Rigel Cross",
"phone": "1-564-651-5621",
"email": "[email protected]",
"dob": "04/04/1969"
},
{
"name": "Jane Carney",
"phone": "658-6123",
"email": "[email protected]",
"dob": "29/10/1971"
},
{
"name": "Chancellor B. Cobb",
"phone": "1-127-360-1239",
"email": "[email protected]",
"dob": "25/09/1969"
},
{
"name": "Desirae Montoya",
"phone": "1-859-784-3087",
"email": "[email protected]",
"dob": "28/08/1991"
},
{
"name": "Rahim P. Whitfield",
"phone": "1-926-984-1942",
"email": "[email protected]",
"dob": "15/06/1978"
},
{
"name": "Brendan Willis",
"phone": "1-208-214-2665",
"email": "[email protected]",
"dob": "31/10/1987"
},
{
"name": "Brady George",
"phone": "1-855-381-7483",
"email": "[email protected]",
"dob": "19/06/1969"
},
{
"name": "Austin Clark",
"phone": "449-1124",
"email": "[email protected]",
"dob": "24/10/1999"
}
]
App.js
import React from 'react';
import DataTable from 'react-data-table-component';
import data from './data.json';
const columns = [
{
name: 'Name',
selector: 'name',
sortable: true,
},
{
name: 'Phone',
selector: 'phone',
sortable: true,
},
{
name: 'Email',
selector: 'email',
sortable: true,
},
{
name: 'DOB',
selector: 'dob',
},
];
function App() {
return (
<div class="App">
<datatable title="Employees" columns={columns} data={data} pagination="" highlightonhover="">
</datatable></div>
);
}
export default App;
4. Output
Run the project and check it in the browser.
Output - How to implement DataTable in React - Clue Mediator
That’s it for today.
Thank you for reading. Happy Coding..!!