My purpose is delete specific table row after click. I have two components and already wrote function to remove row from the table handleDelete()
. The problem is, after click, always the last row is deleted, not the one on which I clicked. Probably I need to pass value, for example props.nameTask
, to parent component but it doesn't work (error in console).
Parent:
class Home extends Component {
constructor(props) {
super(props);
this.state = { data: [] };
this.handleDelete = this.handleDelete.bind(this);
}
// Delete row from table
handleDelete = (index) => {
var array = this.state.data.slice();
array.splice(index, 1)
this.setState({ data: array });
}
render() {
return (
<div>
<TableTasks handleDelete={this.handleDelete} data={this.state.data} header={[{ name: "Task" }, { name: "Delete" }]} />
</div>
);
}}
export default Home;
Child:
const row = (props, i, handleDelete) =>
<TableRow key={`thr-${i}`}>
<TableRowColumn>
{props.nameTask}
</TableRowColumn>
<TableRowColumn className="IconButton">
<IconButton>
<DeleteIcon onClick={handleDelete}/>
</IconButton>
</TableRowColumn>
</TableRow>;
export const TableTasks = ({ data, header, handleDelete }) =>
<Table>
<TableHeader>
<TableRow>
{header.map((x, i) =>
<TableHeaderColumn key={`thc-${i}`}>
{x.name}
</TableHeaderColumn>
)}
</TableRow>
</TableHeader>
<TableBody>
{data.map((x, i) => row(x, i, handleDelete))}
</TableBody>
</Table>;