How to [ Create Table ] in React js?
To create a table in React.js, you can use the `<table>`, `<thead>`, `<tbody>`, `<tr>`, and `<td>` HTML elements.
Here's a simple example using a functional component:
import React from 'react';
const TableComponent = () => {
// Sample data (replace with your data structure)
const tableData = [
{ id: 1, name: 'John', age: 25, city: 'New York' },
{ id: 2, name: 'Alice', age: 30, city: 'San Francisco' },
{ id: 3, name: 'Bob', age: 22, city: 'Chicago' },
// Add more data as needed
];
return (
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
{tableData.map((row) => (
<tr key={row.id}>
<td>{row.id}</td>
<td>{row.name}</td>
<td>{row.age}</td>
<td>{row.city}</td>
</tr>
))}
</tbody>
</table>
);
};
export default TableComponent;
In this example:
- The `thead` element is used for the table header, and `th` elements define the column headers.
- The `tbody` element contains the table rows (`tr`), and the individual cells are created using `td` elements.
You can customize the component based on your data structure and styling preferences. Additionally, if your data is fetched asynchronously (e.g., from an API), you might want to handle the loading state and display a loading spinner until the data is available.
Integrate the "TableComponent" into your main application or any other component where you want to display the table.
import React from 'react';
import TableComponent from './TableComponent';
const App = () => {
return (
<div>
<h1>Your App</h1>
<TableComponent />
</div>
);
};
export default App;
Replace the sample data with your actual data structure.
How to Create Login Page in React js
What is Virtual DOM in React js
How to create a Table in React js
How to Upload Image in React js
How to Implement Search Functionality in React js
How to Connect React js with Node js Backend
How Many Days to Learn React js
How to Use JQuery in React js
What is Context API in React js
How to Use Chatgpt API with Python
Post a Comment