简体   繁体   English

移除 headlessUi 芯片

[英]Remove headlessUi chip

I need to remove a headlessUi after having added it.添加后,我需要删除 headlessUi。

There is one array of elements有一组元素

myChips = [
  {name: "test", id: 0},
  {name: "test", id: 1},
  {name: "test", id: 2},
]

Then i set my chips like follows:然后我设置我的筹码如下:

{myChips &&
  myChips.map(({name, id }) => {
    return (
      <Chip
        name="test"
        value={id}
        key={id}
        label={`${name}- ${id}`}
        onDelete={(e) => console.log(e.target.value)}
      />
    );
  })}

It shows me this:它向我展示了这一点: 在此处输入图片说明

The problem is that i need to delete them.问题是我需要删除它们。 There no seems to get the value of the chip I click on.似乎没有得到我点击的芯片的价值。 In fact when I click on "x" event.target.value is undefined so there is no way to delete the right element from the array.事实上,当我点击“x”时, event.target.value是未定义的,因此无法从数组中删除正确的元素。

How do you think it can be solved?您认为如何解决?

You have to pass the id to the delete function to delete an element.您必须将 id 传递给 delete 函数才能删除元素。 For example-例如-

<Chip
    name="test"
    value={id}
    key={id}
    label={`${name}- ${id}`}
    onDelete={(e) => handleDelete(e, id)}
/>

And at the handleDelete() funciton-handleDelete()函数中-

const handleDelete = (e, id) => {
    const _chips = myChips.filter(chip => chip.id !== id);

    // This is from useState I am assuming here
    setChips(_chips);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM