I click the icon 'delete' on the product. I pull out his index and save it in state. Example: select: 1,index: 1.
How to set this.setState to delete an object nested in an array colors in array products. Example delete object:
{
a: 'orange'
}
from array colors in array products
this.state.select is the item in the products, this.state.index is color in the item to delete
And how does it look in a real application? Give your products and colors id? I would like it to be dynamic. I click the product, download its index and delete
class App extends Component {
constructor(){
super();
this.state {
products: [
{
colors: [{a:'black'}, {a:'orange'}, {a:'purple'}]
desc: 'gfgfg'
},
{
colors: [{a: 'yellow'}, {a: 'white'}, {a:'gray'}],
desc: 'gfgfgfg'
},
{
colors: [{a: 'pink'}, {a: 'brown'}, {a:'green'}],
desc: 'gfgfgfg'
}
],
select: 1 //example
index: 1 //example
}
}
removeItem = () => {
const { select, index } = this.state;
if(index) {
this.setState({
products: [
...this.state.products[select].colors.slice(0, index),
...this.state.products[select].colors.slice(index + 1),
]
});
}
};
render () {
return (
<div>
<ul>
{
this.state.products
.map((product, index) =>
<Product
key={index}
index={index}
product={product}
/>
)
}
</ul>
<Products
/>
</div>
)
}
}
this.state.selectis the item in the products to be deleted, andthis.state.indexthe item undercolorsto be deleted?selectandindex?this.state.selectis the item in the products,this.state.indexis color in the item to delete