I am using map to loop over the array object getting from server, each object is used for one row in a table to show data. And I want to do a particular action for each row by calling function and pass to an index. The code here:
<TableBody>
{productsData.map((product, index) => {
return (
<TableRow key={product.productId}>
<TableCell>
<Button
aria-owns={anchorEl ? `manipulation${index}` : undefined}
aria-haspopup="true"
onClick={handleClick}
className={classes.button}
size="small"
variant="contained"
>
Thao tác
</Button>
<Menu id={`manipulation${index}`} anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={handleClose}>
<MenuItem onClick={**handleOpen(index)**}>Xem trước</MenuItem>
</Menu>
</TableCell>
</TableRow>
)
})}
</TableBody>
The way I declare handleOpen: const handleOpen = (index) => () => {...}
=> I expected the handleOpen will render like that: handleOpen(0) for row 0, handleOpen(1) for row 1. But it's always end up with the last index of array. May be about the closure in javascript but I dont know how to fix
Please give me any suggestion. Thank in advance.