I created a plain dynamic table in react (not using react-table). I want to change the background color of a clicked row. I added ref to table: but I'm not getting access to children ().
How can I change the style of a clicked row by id in a table in react?
import React, { useEffect, useRef } from "react";
import "./TableBox.css";
const TableBox = ({ table_data }) => {
const myTable = useRef(null);
useEffect(() => {
console.log("myTable", myTable.current);
});
const handlerRowClicked = (event) => {
// console.log('clicked')
// myTable.current.backgroundColor='blue'
};
return (
<div>
<table ref={myTable}>
<thead>
<tr>
{Object.keys(table_data[0]).map((title) => (
<th key={title}>{title}</th>
))}
</tr>
</thead>
<tbody>
{table_data.map((row, index) => (
<tr key={index} id={index} onClick={handlerRowClicked}>
{Object.keys(row).map((key) => (
<td key={row[key]}>{row[key]}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
};
export default TableBox;