skip to Main Content

Doing a simple project where I map out from an array and create cards with the object properties. Problem is when I click on the "Not Interested" button(basically a delete post button) I seem to clear out all the posts. Still working out the kinks to react hooks so I apologize if this is a simple problem

import React, {useState} from 'react'
import styled from 'styled-components'

function Cards() {

    const carddata = [{
        name: "Best Of Paris In 7 Days Tour",
        image: "https://res.cloudinary.com/dgpmofizn/image/upload/v1684017660/img-1_xli1dp.jpg",
        description: "Paris is synonymous with the finest things that culture can offer — in art, fashion, food, literature, and ideas. On this tour, your Paris-savvy Rick Steves guide will immerse you in the very best of the City of Light: the masterpiece-packed Louvre and Orsay museums, resilient Notre-Dame Cathedral, exquisite Sainte-Chapelle, and extravagant Palace of Versailles. You'll also enjoy guided neighborhood walks through the city's historic heart as well as quieter moments to slow down and savor the city's intimate cafés, colorful markets, and joie de vivre. Join us for the Best of Paris in 7 Days!",
        price:"$1,995"
    },{
        name: 'Best Of Ireland In 14 Days Tour',
        image: "https://res.cloudinary.com/dgpmofizn/image/upload/v1684017660/img-3_tyhpum.jpg",
        description: "Rick Steves' Best of Ireland tour kicks off with the best of Dublin, followed by Ireland's must-see historical sites, charming towns, music-filled pubs, and seaside getaways — including Kinsale, the Dingle Peninsula, the Cliffs of Moher, the Aran Islands, Galway, Connemara, Giant's Causeway, and the compelling city of Belfast. All along the way, Rick's guides will share their stories to draw you in to the Emerald Isle, and the friendliness of the people will surely steal your heart. Join us for the Best of Ireland in 14 Days!",
        price:"$3,895"
    },{
        name: 'Best Of Salzburg & Vienna In 8 Days Tour',
        image: "https://res.cloudinary.com/dgpmofizn/image/upload/v1684017660/img-4_twyhns.jpg",
        description: "Let's go where classical music, towering castles, and the-hills-are-alive scenery welcome you to the gemütlichkeit of Bavaria and opulence of Austria's Golden Age. Your Rick Steves guide will bring this region's rich history and culture to life in festive Munich, Baroque Salzburg, sparkling Lake Hallstatt, monastic Melk, the blue Danube, and royal Vienna — with cozy villages and alpine vistas all along the way. Join us for the Best of Munich, Salzburg & Vienna in 8 Days",
        price:"$2,695"
    }, {
        name: 'Best Of Poland In 10 Days Tour',
        image: "https://res.cloudinary.com/dgpmofizn/image/upload/v1684017660/img-2_ss0wiu.jpg",
        description: "Starting in the colorful port city of Gdańsk, you'll escape the crowds and embrace the understated elegance of ready-for-prime-time Poland for 10 days. With an expert Rick Steves guide at your side, you'll experience mighty Malbork castle, the cobbly-cute village of Toruń, Poland's contemporary capital of Warsaw, the spiritual Jasna Góra Monastery, and charming Kraków — Poland's finest city. In this land of surprises — so trendy and hip, yet steeped in history — there's so much to discover. Join us for the Best of Poland in 10 Days!",
        price:"$2,595"
    }]

    const [cardinfo, setCardinfo] = useState(carddata)

    const handleClear = () => {
        setCardinfo([]);
    }

  return (
    <Main>
        <div className='whole-cards'>
            {cardinfo.map((carddata) => (
                <div className='card-body' key={carddata.name}>
                    <img src={carddata.image} />
                    <span>{carddata.price}</span>
                    <h3>{carddata.name}</h3>
                    <p>{carddata.description}</p>
                    <button onClick={handleClear}>Not Interested</button>
                </div>
            ))}
        </div>
    </Main>
  )
}

const Main = styled.div`
    width:100%;
    

   .whole-cards{
    display:flex;
    flex-wrap:wrap;
    padding:5px;
    margin:10px;
    
    .card-body{
        border:1px solid black;
        height:700px;
        margin:10px;
        width:350px;
        background-color:white;

        img{
            height:300px;
            width:350px;
            z-index:0;
        }

        span{
            z-index:1;
            border:1px solid green;
            background-color:green;
            color:white;
            position:relative;
            padding:5px;
            top:-298px;
            left:294px;
        }

        h3{
            text-align:center;
        }

        button{
            width:250px;
            color:green;
            position:relative;
            left:40px;
            background-color:white;
            border:1px solid green;
        }
    }
   }
`;

export default Cards

I tried using react hooks but thats causing all the posts to delete.

2

Answers


  1. When you tap on the button you set the state to be an empty array, so it “delete” all post.

    You should add an id or any unique identifier for each card, and pass to the handle function the relevance id.

    The function should filter out the card with this id.

    You can find example in this question in the accepted answer.

    Remove item from useState array

    Login or Signup to reply.
  2. You’re currently setting cardinfo to an empty array, so everything gets removed. Instead, filter the array to remove the element that contained the clicked button.

    const handleClear = (elem) => {
        setCardinfo(cardinfo.filter(o => o !== elem));
    }
    
    <button onClick={() => handleClear(carddata)}>Not Interested</button>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search