
在react应用中,当我们需要从一个数据数组(例如,一个旅行团列表)动态生成一组ui元素(例如,卡片),并为每个元素提供一个删除功能时,一个常见的误区是错误地更新了组件的状态。原始代码中,handleclear 函数的实现如下:
const handleClear = () => {
setCardinfo([]);
}这段代码的问题在于,无论用户点击了哪张卡片的“Not Interested”按钮,它都会调用 setCardinfo([]),这将 cardinfo 状态直接设置为空数组。这意味着,组件会重新渲染,并且由于 cardinfo 现在是空的,所有之前渲染的卡片都会被移除。这显然不是我们期望的“删除单个卡片”的效果。
核心问题在于对React状态更新的理解:React的状态更新应该是不可变的(immutable)。这意味着我们不应该直接修改现有状态数组,而是应该创建一个新的数组,其中包含我们想要保留的元素,然后用这个新数组来更新状态。
为了实现单个卡片的删除,我们需要一种方法来从现有数组中移除一个特定的元素,同时又不改变原始数组。Array.prototype.filter() 方法正是为此而生。
filter() 方法会创建一个新数组,新数组中的元素是那些在回调函数中返回 true 的原数组元素。它不会修改原数组,这完美符合React状态更新的不可变性原则。
要正确地删除单个卡片,我们需要对 handleClear 函数和按钮的 onClick 事件进行修改。
我们需要让 handleClear 函数知道用户点击的是哪张卡片对应的删除按钮。因此,该函数需要接收一个参数,代表要删除的特定卡片数据。
// Cards.js
import React, {useState} from 'react'
import styled from 'styled-components'
function Cards() {
// ... (carddata 保持不变)
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-Chapel...",
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...",
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...",
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...",
price:"$2,595"
}]
const [cardinfo, setCardinfo] = useState(carddata)
// 修改后的 handleClear 函数
const handleClear = (itemToDelete) => {
// 使用 filter 方法创建一个新数组,不包含 itemToDelete
setCardinfo(cardinfo.filter(card => card !== itemToDelete));
}
return (
<Main>
<div className='whole-cards'>
{cardinfo.map((carddata) => (
<div className='card-body' key={carddata.name}>
<img src={carddata.image} alt={carddata.name} />
<span>{carddata.price}</span>
<h3>{carddata.name}</h3>
<p>{carddata.description}</p>
{/* 步骤二:更新按钮的 onClick 事件 */}
<button onClick={() => handleClear(carddata)}>Not Interested</button>
</div>
))}
</div>
</Main>
)
}
// ... (styled components 保持不变)
const Main = styled.div`
width:100%;
/* ... 样式代码 ... */
`;
export default Cards;在上述代码中:
在 map 循环内部,我们需要确保当按钮被点击时,将当前迭代的 carddata 对象传递给 handleClear 函数。
<button onClick={() => handleClear(carddata)}>Not Interested</button>这里使用了箭头函数 () => handleClear(carddata)。这是因为 map 循环会在渲染时立即执行 onClick 属性的值。如果我们直接写 onClick={handleClear(carddata)},那么 handleClear 会在组件渲染时立即执行,而不是在按钮被点击时执行,并且 carddata 会被立即删除。通过使用箭头函数,我们创建了一个新的函数,它只在按钮实际被点击时才调用 handleClear 并传入 carddata。
通过本教程,我们学习了如何在React中,使用useState和Array.prototype.filter()方法,有效地从一个映射生成的UI列表中删除单个元素。理解并实践状态更新的不可变性原则是构建高效、可维护React应用的关键。掌握filter()等数组方法,能够帮助开发者更精确地控制组件状态,从而实现更精细的用户交互和数据管理。
以上就是React Hooks中从数组映射生成的卡片中删除单个元素的正确姿势的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号