I created a custom component <Card />. On the main page, I have add a button which will add these cards once the cards are added each card have a specific remove button which should remove that specific card from a bunch of cards.
Here is my code:
Add function to add cards:
handleAddComponent() {
this.setState({
component: [
...this.state.component,
<Card remove={this.handleRemoveRow} />
]
});
}
Remove function to remove cards:
handleRemoveRow(event) {
// console.log(event.target.);
this.setState({
component: this.state.component.slice(0, -1)
});
}
and the render:
render() {
return (
<section>
{this.state.component}
<button onClick={this.handleAddComponent}>
<span>Add</span>
</button>
</section>
);
}
The issue with this code is whenever I click the remove button only the last component is removed which doesn't meet my condition, I want to remove a particular card. component: this.state.component.slice(0, -1) this removes the last component.
Is there any other way I can get the index of each card component so I can remove a particular card?