值得一看
双11 12
广告
广告

React Hooks中从数组映射生成的卡片中删除单个元素的正确姿势

React Hooks中从数组映射生成的卡片中删除单个元素的正确姿势

本教程旨在解决React应用中,当从数组映射生成UI卡片时,如何正确删除单个卡片而非清空所有卡片的问题。通过详细解析useState与Array.prototype.filter()的结合使用,我们将展示如何以不可变的方式更新组件状态,从而实现精确的元素删除,避免常见的清空列表错误,提升React组件的交互性和数据管理效率。

问题剖析:为何所有卡片都被删除了?

在react应用中,当我们需要从一个数据数组(例如,一个旅行团列表)动态生成一组ui元素(例如,卡片),并为每个元素提供一个删除功能时,一个常见的误区是错误地更新了组件的状态。原始代码中,handleclear 函数的实现如下:

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

这段代码的问题在于,无论用户点击了哪张卡片的“Not Interested”按钮,它都会调用 setCardinfo([]),这将 cardinfo 状态直接设置为空数组。这意味着,组件会重新渲染,并且由于 cardinfo 现在是空的,所有之前渲染的卡片都会被移除。这显然不是我们期望的“删除单个卡片”的效果。

核心问题在于对React状态更新的理解:React的状态更新应该是不可变的(immutable)。这意味着我们不应该直接修改现有状态数组,而是应该创建一个新的数组,其中包含我们想要保留的元素,然后用这个新数组来更新状态。

核心解决方案:利用 Array.prototype.filter()

为了实现单个卡片的删除,我们需要一种方法来从现有数组中移除一个特定的元素,同时又不改变原始数组。Array.prototype.filter() 方法正是为此而生。

filter() 方法会创建一个新数组,新数组中的元素是那些在回调函数中返回 true 的原数组元素。它不会修改原数组,这完美符合React状态更新的不可变性原则。

实现步骤与代码示例

要正确地删除单个卡片,我们需要对 handleClear 函数和按钮的 onClick 事件进行修改。

步骤一:修改删除逻辑函数 handleClear

我们需要让 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}>
@@##@@
<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;

在上述代码中:

  • handleClear 函数现在接受一个参数 itemToDelete。
  • 我们使用 cardinfo.filter(card => card !== itemToDelete) 来生成一个新的数组。这个新数组会排除掉与 itemToDelete 相等的那个卡片对象。
  • 最后,通过 setCardinfo() 更新状态为这个过滤后的新数组。

步骤二:更新按钮的 onClick 事件

在 map 循环内部,我们需要确保当按钮被点击时,将当前迭代的 carddata 对象传递给 handleClear 函数。

<button onClick={() => handleClear(carddata)}>Not Interested</button>

这里使用了箭头函数 () => handleClear(carddata)。这是因为 map 循环会在渲染时立即执行 onClick 属性的值。如果我们直接写 onClick={handleClear(carddata)},那么 handleClear 会在组件渲染时立即执行,而不是在按钮被点击时执行,并且 carddata 会被立即删除。通过使用箭头函数,我们创建了一个新的函数,它只在按钮实际被点击时才调用 handleClear 并传入 carddata。

注意事项与最佳实践

  1. 状态更新的不可变性: 始终记住,在React中更新数组或对象状态时,不要直接修改原始状态变量。而是应该创建新的数组或对象,然后用新的值来更新状态。filter, map, slice, … (展开运算符) 都是创建新数组/对象的常用方法。
  2. key 属性的重要性: 在使用 map 渲染列表时,为每个列表项提供一个稳定且唯一的 key 属性至关重要。这有助于React识别哪些项被添加、移除或重新排序,从而优化渲染性能并避免潜在的bug。在本例中,key={carddata.name} 被用作 key。虽然在当前数据集中 name 可能是唯一的,但在实际应用中,如果数据源包含唯一的ID(例如 carddata.id),使用ID作为 key 会是更健壮的选择。
  3. 事件处理函数参数传递: 当在循环中为子组件或元素绑定事件,并且需要传递当前循环项作为参数时,务必使用箭头函数 () => yourFunction(arg) 的形式,以确保函数在事件发生时才被调用。

总结

通过本教程,我们学习了如何在React中,使用useState和Array.prototype.filter()方法,有效地从一个映射生成的UI列表中删除单个元素。理解并实践状态更新的不可变性原则是构建高效、可维护React应用的关键。掌握filter()等数组方法,能够帮助开发者更精确地控制组件状态,从而实现更精细的用户交互和数据管理。

{carddata.name}

温馨提示: 本文最后更新于2025-07-18 22:40:57,某些文章具有时效性,若有错误或已失效,请在下方留言或联系易赚网
文章版权声明 1 本网站名称: 创客网
2 本站永久网址:https://new.ie310.com
1 本文采用非商业性使用-相同方式共享 4.0 国际许可协议[CC BY-NC-SA]进行授权
2 本站所有内容仅供参考,分享出来是为了可以给大家提供新的思路。
3 互联网转载资源会有一些其他联系方式,请大家不要盲目相信,被骗本站概不负责!
4 本网站只做项目揭秘,无法一对一教学指导,每篇文章内都含项目全套的教程讲解,请仔细阅读。
5 本站分享的所有平台仅供展示,本站不对平台真实性负责,站长建议大家自己根据项目关键词自己选择平台。
6 因为文章发布时间和您阅读文章时间存在时间差,所以有些项目红利期可能已经过了,能不能赚钱需要自己判断。
7 本网站仅做资源分享,不做任何收益保障,创业公司上收费几百上千的项目我免费分享出来的,希望大家可以认真学习。
8 本站所有资料均来自互联网公开分享,并不代表本站立场,如不慎侵犯到您的版权利益,请联系79283999@qq.com删除。

本站资料仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!
THE END
喜欢就支持一下吧
点赞15赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容