【React】使用react hooks实现评论示例

本文详细描述了如何使用React实现一个包含评论列表、导航栏切换、发布评论、删除评论等功能的组件,涉及useState和useRef等状态管理工具的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态效果展示

实现功能

1、渲染评论列表
2、删除评论
3、渲染导航栏和高亮
4、评论列表排序功能
5、获取评论
6、点击发布按钮发布评论
7、清空输入框
8、重新聚焦

实现代码

1、需要引入

import React, {
    useRef, useState } from 'react'
import avatar from "../logo.png" //头像
import "./css/index.css" //样式

2、样式----[./css/index.css]

* {
   
    margin: 0;
    padding: 0;
}

.comment-box {
   
    width: 1000px;
    margin: 20px auto;
    height: 200px;
}

/* 导航栏 */
.comment-tabs {
   
    display: flex;
    align-items: end;
    font-weight: normal;
    margin-bottom: 20px;
}

.tabs-left {
   
    margin-right: 20px;
    font-size: 14px;
}

.tabs-left p span {
   
    margin-left: 6px;
    color: #666;
    font-size: 10px;
}

.tabs-right {
   
    display: flex;
    color: #666;
    font-size: 10px;
}

.tabs-right .active {
   
    color: #08a17d;
    font-weight: 500;
}


.tabs-right div span {
   
    display: inline-block;
    margin: 0 6px;
    height: 6px;
    border-left: 1px solid #666;
}

.tabs-right div:last-child span {
   
    border: none;
}

/* 发表评论 */
.comment-send {
   
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 40px;
}

.avatar {
   
    width: 35px;
    height: 35px;
    margin-left: 10px;
    overflow: hidden;
    border-radius: 35px;
    border: 1px solid #08a17d;
}

.avatar img {
   
    width: 100%;
    height: 100%;
}

.comment-
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值