I am trying to render a table using gridjs under react and attach an eventListener to tables cells. My code is partially working, but when I click a given cell, I got repeated events, probably one for each row in the table for that event. You can see below, the event is able to tell which column was clicked but it looks it fires for all rows in table.
import React, { useEffect, useRef } from "react";
import { Grid as Gridjs } from "gridjs";
function App() {
const wrapperRef = useRef(null);
const grid = new Gridjs({
columns: ["Name", "Email", "Phone Number"],
data: [
["John", "john@example.com", "(353) 01 222 3333"],
["Mark", "mark@gmail.com", "(01) 22 888 4444"]
]
});
useEffect(() => {
grid.render(wrapperRef.current);
wrapperRef.current.addEventListener("click", (ev) => {
if (ev.target.tagName === "TD") {
console.log(ev.target.dataset.columnId);
}
});
});
return (
<div className="App">
<header className="App-header"></header>
<body>
<div ref={wrapperRef} />
</body>
</div>
);
}
export default App;
Here is the CodeSandBox
Any idea what am I doing wrong or lacking here?