Hi I know this is a very basic problem, but my code doesn't want to work correctly. I've tried removing some items from an array stored in this.state but the function just do different things than I expect, sometimes it removes wrong item and recently it started removing more than one item at once, can someone here please review my code and see what's missing?
deleteProduct = async (index) => {
this.setState({ loading: true, items: [] })
let { cart } = this.state
cart.splice(index, 1)
console.log('deleted this > :', cart.splice(index, 1));
this.setState({cart:cart})
try {
await AsyncStorage.setItem('cart', JSON.stringify(cart))
this.setState({ cart: cart, loading: false })
this.retrieveCart()
} catch (error) {
this.setState({ error: error })
console.log(error.message)
}
}
my data looks like this
cart : Array [
Object {
"id": 195,
"price": "69",
"qty": 1,
},
Object {
"id": 200,
"price": "69",
"qty": 1,
},
Object {
"id": 201,
"price": "110",
"qty": 1,
},
]
should I just use a different approach like targeting by id because that index thing is just not working well