In my state I have an array, which I mapped in render
function:
State
this.state = {
newItem: {
name: "",
amount: 0
},
products: [
{
name: "Item",
amount: 5
},
{
name: "Item",
amount: 5
},
{
name: "Item",
amount: 5
}
]
}
I want to delete element from the array products by clicking in button of that element. But what I attempted to do, everything fails. I searched a lot and find logically the most common solution but it didn't work too. Here is the delete function:
Delete function
delete(e) {
this.setState(prevState => ({ products: prevState.products.filter((product) => {
return product !== e.target.value
})}))
console.table(this.state.products)
}
Mapped JSX code
{
this.state.products.map((item, index) => {
return(
<div key={index}>
<input readOnly value={this.state.products[index].name} type="text" />
<button disabled>-</button>
<input readOnly value={this.state.products[index].amount} type="number" />
<button disabled>+</button>
<button onClick={(e) => this.delete(e)}>Delete</button>
</div>
)
})
}