In my component I have a Table with generated rows from some custom array of objects. In the last TableCell I want to have a icon button that on click opens Menu with some MenuItem actions (edit and delete). This is my code:
{folders.map(folder => {
return (
<TableRow key={folder.id} >
<TableCell>{folder.name}</TableCell>
<TableCell>
<IconButton
onClick={this.handleFolderActionClick}>
<MoreHoriz />
</IconButton>
<Menu onClose={this.handleFolderActionClose} >
<MenuItem onClick={event => {onEditFolder(event, folder)}}>
<ListItemIcon>
<Edit />
</ListItemIcon>
<ListItemText inset primary="Edit" />
</MenuItem>
<MenuItem onClick={{event => onDeleteFolder(event, folder)}}>
<ListItemIcon>
<Delete />
</ListItemIcon>
<ListItemText inset primary="Delete" />
</MenuItem>
</Menu>
</TableCell>
</TableRow>
);
})}
The onClick event is always passing the last folder element in the array and not the one mapped to that particular TableRow.
I've read that MenuItem onClick event should not be used in this manner but I don't have any other idea how to solve my specific problem. I'm opened for any suggestions. How to pass object from the outer map function to onClick event of ManuItem?
Edit: Sandbox example