I am new in React and a little bit confused regarding to reusable components. I create my pages e.g. Employee in a pages folder and keep Material UI components e.g. Datatable in the components folders.
Normally I would copy Datatable to my pages folder as EmployeDatatable and customize it based on the columns for employees and fetch data in this EmployeDatatable.
On the other hand, in order to prevent code repeat and managing components easily, I am also thinking to call the same Datatable for different pages e.g. Employee, Salary by passing the parameters like columns, data urls,...
For example, when I am on the list page, I can call Datatable by passing all the necessary parameters:
const EmployeeList = () => {
return (
<div className="list">
<Sidebar/>
<div className="listContainer">
<Navbar/>
<Datatable/>
</div>
</div>
)
}
export default EmployeeList
But I am not sure if it is a proper approach and makes something much complicated. Ok, maybe at the beginning it may not be good idea, but I would be happy if you share your ideas based on what should be for a proper implementation (I have developed several React apps and where I created seperated Datatables instead of reusable and now I can start to build reusable components if needed).
Could you pls clarify me how should I build? my components for this scenario?
Here is Datatable part below if it is needed to have a look at:
const Datatable = () => {
const [data, setData] = useState(userRows);
const handleDelete = (id) => {
setData(data.filter((item) => item.id !== id));
};
return (
<div className="datatable">
// ...
</div>
);
};
export default Datatable;