I have a table with some fields and I want to put a trash icon to each row and then delete that row when it is clicked. I have a problem on that row to show that icon and send customerId field of the corresponding row to the delete function.
I copied the trash icon from bootstrap site Here is the code:
import React, { useEffect, useState } from 'react';
import * as ReactBootStrap from 'react-bootstrap';
import * as service from '../service/FetchCustomerService';
import Button from 'react-bootstrap/Button'
function MainPanel() {
const [customers, setCustomers] = useState([]);
useEffect(() => {
const fetchPostList = async () => {
const response = await service.getCustomerList();
setCustomers(response.data);
console.log(response.data)
};
fetchPostList()
}, []);
const deleteCustomer = (customerId) => {
// CALL DELETE FUNCTION WITH CUSTOMERID
console.log(customerId);
}
return (
<ReactBootStrap.Table striped bordered hover>
<thead>
<tr>
<th>ID</th>
<th>FirstName</th>
<th>LastName</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{customers &&
customers.map((item) => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstName}</td>
<td>{item.lastName}</td>
//problem is the following line
<td><Button onClick={this.deleteCustomer(this.customerId)} className='bi-trash'>Delete</Button></td>
</tr>
))}
</tbody>
</ReactBootStrap.Table>
);
}
export default MainPanel;
But with this, only button is coming without an icon and I cannot pass the customerId to the function, error is:
MainPanel.js:45 Uncaught TypeError: Cannot read properties of undefined (reading 'deleteCustomer')
How can I solve that ?
deleteCustomerfunction with an argument, you might have to pass in an anonymous function to call it like() => deleteCustomer(customerId)