I'm using this code to remove a item from Array in React Native:
var array = [...this.state.gallery];
if (index !== -1) {
array.splice(index, 1);
this.setState({gallery: array,
photosHaveNow: this.state.photosHaveNow - 1});
}
But, when item is removed, that array is write in your place:
{"empty": true, "key": "blank-2"}
Final example:
["http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33-1.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33-2.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33-3.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.34.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.34-1.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.34-4.jpeg", {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}]
When an item is removed through the splice, "{" empty ": true," key ":" blank-2 "}" is put in place, I wish nothing was put in place.
I believe I have found a solution. I don't know if it sounds good.
var array = [...this.state.gallery];
if (index !== -1) {
array.splice(index, 1);
}
var finalArray = [];
array.forEach(element => {
if(typeof element !== 'object')
{
finalArray = finalArray.concat(element)
}
})
this.setState({
gallery: finalArray,
photosHaveNow: this.state.photosHaveNow - 1
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.