How to Delete Component ?

I wanted to delete the card that is displayed in the image, when you click on the "delete" button. But this isn't not working from what I read from the article : https://www.codingbeautydev.com/blog/react-remove-element-onclick Code: https://paste.ofcode.org/xdjSVt6TsrqGCQS9e8CN7r
How to Remove an Element OnClick in React - Coding Beauty
Learn how to easily remove an element onclick in React, whether it's in a list or it's a standalone element.
10 Replies
MarkBoots
MarkBoots12mo ago
I'm no react expert. but on line 18. Shouldn't that be setComponents(newComponents) instead of setFruits(newComponents)? think you forgot to change that from the example
Leo_
Leo_OP12mo ago
yeah i changed that, but and fixed other things but it doens't delete the card i selected rather deletes the last card from the components array
dys 🐙
dys 🐙12mo ago
I looked at your code, thought it would delete the correct component, and testing it shows it works.
MarkBoots
MarkBoots12mo ago
yea, i checked it as well https://playcode.io/1704854
Leo_
Leo_OP12mo ago
PlayCode.io
JavaScript Playground
Try this online JavaScript Playground playground with instant live preview and console. Easy & Fast. Experiment yourself.
Leo_
Leo_OP12mo ago
is there a problem with my CARD ? cause I use the exactly same code now right
dys 🐙
dys 🐙12mo ago
You are creating components, but you lack the code to insert those components into the page's DOM. I restructured your code minimally & added an index.jsx that does that insertion.
Leo_
Leo_OP12mo ago
https://playcode.io/1704936 I fixed this problem of deleting, but only problem now exists is
PlayCode.io
Simple React Component Test
Try this online Simple React Component Test playground with instant live preview and console. Easy & Fast. Experiment yourself.
Leo_
Leo_OP12mo ago
after you edit the card, and type in something..and you delete any other card, the value in the edited card changes to default when after the deletion. Meaning the text doesn't get saved after deletion before deletion : after deletion: .
Jochem
Jochem12mo ago
Don't ping people just to get them to pay attention to your post again please. If you have something to add or are replying to a question they asked, that's fine but this is just spam.
Want results from more Discord servers?
Add your server