When displaying data in an organized style in web applications, tables are a popular component. Using the strength of JSX and JavaScript, you can quickly construct dynamic and interactive tables in ReactJS. The process of making a table in ReactJS will be covered in this post, along with some best practices. In this article, we will create a simple table in React.js just like you would create in a normal HTML project.Â
Prerequisites: The pre-requisites for this project are:
Creating a React application:
Step 1: Create a react application by typing the following command in the terminal.
npx create-react-app react-table
Step 2: Now, go to the project folder i.e react-table by running the following command.
cd react-table
Project Structure: It will look like the following:
Example 1: Here App.js is the default component. At first, we will see how to create a table using the hardcoded values. Later we will see how to dynamically render the data from an array inside the table.Â
Filename: App.js
Javascript
import './App.css';Â
function App() {return (<div className="App">Â Â Â Â <table>Â Â Â Â Â Â Â Â <tr>Â Â Â Â Â Â Â Â Â Â Â Â <th>Name</th>Â Â Â Â Â Â Â Â Â Â Â Â <th>Age</th>Â Â Â Â Â Â Â Â Â Â Â Â <th>Gender</th>Â Â Â Â Â Â Â Â </tr>Â Â Â Â Â Â Â Â <tr>Â Â Â Â Â Â Â Â Â Â Â Â <td>Anom</td>Â Â Â Â Â Â Â Â Â Â Â Â <td>19</td>Â Â Â Â Â Â Â Â Â Â Â Â <td>Male</td>Â Â Â Â Â Â Â Â </tr>Â Â Â Â Â Â Â Â <tr>Â Â Â Â Â Â Â Â Â Â Â Â <td>Megha</td>Â Â Â Â Â Â Â Â Â Â Â Â <td>19</td>Â Â Â Â Â Â Â Â Â Â Â Â <td>Female</td>Â Â Â Â Â Â Â Â </tr>Â Â Â Â Â Â Â Â <tr>Â Â Â Â Â Â Â Â Â Â Â Â <td>Subham</td>Â Â Â Â Â Â Â Â Â Â Â Â <td>25</td>Â Â Â Â Â Â Â Â Â Â Â Â <td>Male</td>Â Â Â Â Â Â Â Â </tr>Â Â Â Â </table></div>);}Â
export default App; |
In the above example, we just simply used the HTML table elements which are <table>, <tr>, <th>, and <td> elements.Â
Example 2: Now let us see how we can dynamically render data from an array. Instead of manually iterating over the array using a loop, we can simply use the inbuilt Array.map() method. The Array.map() method allows you to iterate over an array and modify its elements using a callback function. The callback function will then be executed on each of the array’s elements. In this case, we will just return a table row on each iteration.
Filename: App.js
Javascript
import './App.css';Â
// Example of a data array that// you might receive from an APIconst data = [    { name: "Anom", age: 19, gender: "Male" },    { name: "Megha", age: 19, gender: "Female" },    { name: "Subham", age: 25, gender: "Male" },]Â
function App() {Â Â Â Â return (Â Â Â Â Â Â Â Â <div className="App">Â Â Â Â Â Â Â Â Â Â Â Â <table>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <tr>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <th>Name</th>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <th>Age</th>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <th>Gender</th>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â </tr>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â {data.map((val, key) => {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return (Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <tr key={key}>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <td>{val.name}</td>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <td>{val.age}</td>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <td>{val.gender}</td>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â </tr>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â )Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â })}Â Â Â Â Â Â Â Â Â Â Â Â </table>Â Â Â Â Â Â Â Â </div>Â Â Â Â );}Â
export default App; |
Filename: App.css Now, let’s edit the file named App.css to style the table.
CSS
.App {Â Â Â Â width: 100%;Â Â Â Â height: 100vh;Â Â Â Â display: flex;Â Â Â Â justify-content: center;Â Â Â Â align-items: center;}Â
table {Â Â Â Â border: 2px solid forestgreen;Â Â Â Â width: 800px;Â Â Â Â height: 200px;}Â
th {Â Â Â Â border-bottom: 1px solid black;}Â
td {Â Â Â Â text-align: center;} |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

