I have a WordPress Gutenberg block that I'm able to add items to an array in. However, I need to provide the user a way to remove a specific list item from the array of items when clicking a button next to the specific item. The data is stored as an array of objects. Outside of WordPress and Gutenberg, this will work on a basic level of removing an item by index from an array. However, the following not only doesn't remove an item, it is also blocking adding new items.
Markup used to trigger delete function
{items.map((item, index) => {
return (
<Button
onClick={removeElement(index)}
/>
<RichText.Content
value={item['listItem']}
/>
)
})}
Used to add new empty item to list
const addListItem = () => {
setAttributes({
items: items.concat({
listItem: ''
})
});
};
How I'm currently attempting to remove the selected object from the array
const removeListItem = index => {
items.splice(index, 1);
return items;
};
I have seen suggestions of using array.filter
, but will not remove the index from the array.