I am working on a project in react. Where most of the data on different tabs have a table. So I design the "TableItems" component and passed the data according to the component. Right now based on params I toggle the table data. Is this the correct way? some tables have different columns so the data table is varying.
import React, { useState } from "react";
import { DoctorData, PatientData } from "./data";
import "./styles.css";
import {
BrowserRouter as Router,
Routes,
Route,
Link,
useLocation
} from "react-router-dom";
export const Doctor = () => {
const header = ["ID#", "Name", "Disease", "Doctor", "Contact"];
return (
<div className="split">
<Link to="/patient">Paitent</Link>
<TableItems header={header} rowData={DoctorData} />
</div>
);
};
export const Patient = () => {
const header = ["ID#", "Name", "Specialization", "Email", "Contact"];
return (
<div className="split">
<Link to="/">Doctor</Link>
<TableItems header={header} rowData={PatientData} />
</div>
);
};
export const TableItems = ({ header, rowData }) => {
const location = useLocation();
return (
<div className="doctors-list">
<table>
<thead>
<tr>
<th className="checkbox-rect">
<input type="checkbox" id="checkbox-rect1" name="check" />
<label htmlFor="checkbox-rect1">
<span></span>
</label>
</th>
{header.map((data, index) => (
<th key={index}>{data}</th>
))}
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{location.pathname == "/" &&
rowData &&
rowData.map((data, index) => (
<tr key={index}>
<td className="checkbox-rect">
<input type="checkbox" id="checkbox-rect1" name="check" />
<label htmlFor="checkbox-rect1">
<span></span>
</label>
</td>
<td>{data.id}</td>
<td>{data.name}</td>
<td>{data.specs}</td>
<td>{data.email}</td>
<td>{data.contact}</td>
<td>
<i className="fa-solid fa-pen"></i>
</td>
<td>
<i className="fa-solid fa-trash-can"></i>
</td>
</tr>
))}
{location.pathname == "/patient" &&
rowData &&
rowData.map((data, index) => (
<tr key={index}>
<td className="checkbox-rect">
<input type="checkbox" id="checkbox-rect1" name="check" />
<label htmlFor="checkbox-rect1">
<span></span>
</label>
</td>
<td>{data.id}</td>
<td>{data.name}</td>
<td>{data.disease}</td>
<td>{data.doctor}</td>
<td>{data.contact}</td>
<td>
<i className="fa-solid fa-pen"></i>
</td>
<td>
<i className="fa-solid fa-trash-can"></i>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
};
////// MAIN APP LAYOUT
export default function App() {
return (
<>
<Router>
<Routes>
<Route path="/" element={<Doctor />} />
<Route path="/patient" element={<Patient />} />
</Routes>
</Router>
</>
);
}