I'm trying to add a class to ONE specific element. The user clicks a remove button, then the entire row containing the button in a table should appear disabled after a class "removed" is added to the containing row div. There are multiple rows created using array.map. I tried this: How to access/override the className of an element using refs? But useRef targets only the last version of the div created in the iteration, useState for conditional className changes all iterations of that div.
const AccordionBody = ({...props }) => {
const [data, setData] = useState(null);
//data is loaded from api
// set state
...
const handleRemove = (id) => {
// ... sending remove ID to api
// add class "removed" to grandparent of button
};
return (
{data.list.map((item) => (
// change this parent div. Tried useRef and conditional className here.
<div className={`c-table__row`} key={item.id} >
<div className="c-table__row__item">
// this is the button triggering the change
<div className="e-btn__x" onClick={(e) => handleRemove(item.id)}></div>
<img className="u-img--round" src={item.icon}/>
<span className="c-table__cell">
{item.name}
</span>
</div>
<div className="c-table__row__item ">
{item.location}
</div>
//more info in this row
...
</div>
))}
}