如何从数组映射到卡片时删除单个卡片而不是全部卡片?

huangapple go评论70阅读模式
英文:

When mapping out from an array onto a card, how do i delete an individual card and not all of them?

问题

Here is the translated part of your text without the code:

在一个简单的项目中,我从数组中映射并创建具有对象属性的卡片。问题是,当我点击“不感兴趣”按钮(基本上是删除帖子按钮)时,似乎会清除所有帖子。我仍在解决React钩子的问题,如果这是一个简单的问题,我为此道歉。

请让我知道如果您需要更多的帮助。

英文:

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.

答案1

得分: 0

当您点击按钮时,您将状态设置为空数组,因此它会“删除”所有帖子。

您应该为每张卡片添加一个id或任何唯一标识符,并将其传递给处理函数的相关id。

该函数应该过滤掉具有此id的卡片。

您可以在已接受答案中找到此问题的示例。

从useState数组中删除项目

英文:

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

答案2

得分: 0

你目前正在将cardinfo设置为空数组,因此会移除所有内容。而是,筛选该数组以移除包含被点击按钮的元素。

const handleClear = (elem) => {
    setCardinfo(cardinfo.filter(o => o !== elem));
}
<button onClick={() => handleClear(carddata)}>不感兴趣</button>
英文:

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) =&gt; {
    setCardinfo(cardinfo.filter(o =&gt; o !== elem));
}
&lt;button onClick={() =&gt; handleClear(carddata)}&gt;Not Interested&lt;/button&gt;

huangapple
  • 本文由 发表于 2023年5月14日 07:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定