【React】实现消息列表的删除

工作小记,第一次接触react项目


1.增加删除对话项的函数
hooks\use-conversation.ts

  // 删除对话项的函数
  const deleteConversation = (id: string) => {
    setConversationList(prevList => prevList.filter(item => item.id !== id))
  }
  return {
	deleteConversation,
	...
}

2.父组件中通过props解构出deleteConversation并传入子组件Sidebar
app\components\index.tsx

const renderSidebar = () => {
    if (!APP_ID || !APP_INFO || !promptConfig)
      return null
    return (
      <Sidebar
        list={conversationList}
        onCurrentIdChange={handleConversationIdChange}
        currentId={currConversationId}
        copyRight={APP_INFO.copyright || APP_INFO.title}
        onDelete={deleteConversation} // 传入 deleteConversation 函数
      />
    )
  }

3.子组件Sidebar中传入方法,实现删除功能

const Sidebar: FC<ISidebarProps> = ({
  copyRight,
  currentId,
  onCurrentIdChange,
  list,
  onDelete,
}) => {
  const { t } = useTranslation()
  const handleDelete = (id: string) => {
    //console.log('Deleting conversation with id:', id)
    onDelete(id)
  }
  ...

修改消息列表的显示

 <div className="flex items-center justify-between w-full">
                <ItemIcon
                  className={classNames(
                    isCurrent
                      ? 'text-primary-600'
                      : 'text-gray-400 group-hover:text-gray-500',
                    'mr-3 h-5 w-5 flex-shrink-0',
                  )}
                  aria-hidden="true"
                />
                {item.name}
                <TrashIcon
                  onClick={() => handleDelete(item.id)}
                  className="h-5 w-5 text-blue-400 hover:text-blue-700 cursor-pointer"
                  aria-hidden="true"
                />
              </div>

效果:
在这里插入图片描述
但是消息列表式存在服务器上,页面一刷新之后消息列表又出现了,明天还得对齐接口。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现一个简单的品牌管理列表,你可以使用React来建前端界面。下面是一个简单的示例代码,演示如何使用React实现品牌管理列表: ```jsx import React, { useState } from 'react'; function BrandList() { const [brands, setBrands] = useState([]); const handleAddBrand = () => { const brandName = prompt('请输入品牌名称'); if (brandName) { const newBrand = { id: Date.now(), name: brandName }; setBrands([...brands, newBrand]); } }; const handleDeleteBrand = (id) => { const updatedBrands = brands.filter(brand => brand.id !== id); setBrands(updatedBrands); }; return ( <div> <h2>品牌列表</h2> <button onClick={handleAddBrand}>添加品牌</button> <ul> {brands.map(brand => ( <li key={brand.id}> {brand.name} <button onClick={() => handleDeleteBrand(brand.id)}>删除</button> </li> ))} </ul> </div> ); } export default BrandList; ``` 在这个示例中,我们使用了React的函数组件和`useState`钩子来管理品牌列表的状态。在`brands`数组中存储品牌对象,每个对象有一个唯一的`id`和品牌名称`name`。 组件渲染时,会展示一个标题、一个添加品牌的按钮和一个品牌列表。点击添加品牌按钮时,会弹出一个对话框要求输入品牌名称,然后将新品牌添加到`brands`数组中。每个品牌都会显示在列表中,并且每个品牌后面有一个删除按钮,点击按钮会从列表删除该品牌。 这只是一个简单的示例,你可以根据自己的需求进行扩展和修改。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jerry_ww

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值