javascript 当从一个数组Map到一张卡片上时,我如何删除一张卡片而不是所有卡片?

qv7cva1a  于 2023-05-16  发布在  Java
关注(0)|答案(2)|浏览(99)

做一个简单的项目,我从一个数组中Map出来,并创建具有对象属性的卡片。问题是,当我点击“不感兴趣”按钮(基本上是一个删除帖子按钮)时,我似乎清除了所有的帖子。仍在制定扭结React挂钩,所以我道歉,如果这是一个简单的问题

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

我试着使用React挂钩,但这导致所有的职位删除。

v09wglhw

v09wglhw1#

您当前正在将cardinfo设置为空数组,因此所有内容都将被删除。而是筛选数组以移除包含单击的按钮的元素。

const handleClear = (elem) => {
    setCardinfo(cardinfo.filter(o => o !== elem));
}
<button onClick={() => handleClear(carddata)}>Not Interested</button>
6ju8rftf

6ju8rftf2#

当你点击按钮时,你将状态设置为空数组,因此它“删除”所有帖子。
您应该为每张卡片添加一个id或任何唯一标识符,并将相关性id传递给handle函数。
该函数应过滤掉具有此ID的卡。
你可以在这个问题的公认答案中找到例子。
Remove item from useState array

相关问题