简体   繁体   中英

Deleting item (card) from react array

I'm struggling on how to write a delete method to delete a card from this view using the delete button in the kebab dropdown.

https://codesandbox.io/s/weathered-worker-0y5vm

I've looked at all the questions on here about how to remove an item from a react array, but I'm declaring my array later (rather than in the state) with:

{Array.apply(0, Array(1)).map((x, i) => ( ...

All the answers to those questions I've seen use:

.map(item => ( ... `

with the .splice or the .filter method, neither of which I've been able to figure out, since I'm also using Array.apply with some other parameters map needs to take in.

For example, this didn't work but it's exactly what I need to do: Editing and deleting a card in semantic UI using react.

How can I get the delete button to work using either .filter or .splice with the way I create my array currently?

Any suggestions would be greatly appreciated. Thank you!

Try

state.filter( item => item.id !== action.payload);

action.payload could be whatever you send with an onClick or other event handler that matches the item.id or what you choose to delete.

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.

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