当前位置: 首页 > news >正文

免费编程网站seo千享科技

免费编程网站,seo千享科技,网站样式模板,网站建设公司专业一、效果展示 默认效果,一开始默认按照最热进行排序 发布了一条评论 按照最新进行排序 按照最新进行排序 二、效果说明 页面上默认有3条评论,且一开始进入页面的时候是按照点赞数量进行倒序排列展示,可以点击【最热 、最新】进行排序的切换。…

一、效果展示

默认效果,一开始默认按照最热进行排序
发布了一条评论
按照最新进行排序
按照最新进行排序

二、效果说明

页面上默认有3条评论,且一开始进入页面的时候是按照点赞数量进行倒序排列展示,可以点击【最热 、最新】进行排序的切换。

在文本框中输入要评论的文本,然后点击【发布】按钮,即可将评论添加到下方的评论列表当中进行展示;如果没有输入任何文本的时候直接点击【发布】按钮会弹出提示对话框。

点击删除按钮可以将对应的评论从评论列表中移除。

三、涉及知识点

3.1 useState

3.1.1 基础使用

useState 是一个 React Hook(函数),它允许我们向组件添加一个状态变量, 从而控制影响组件的渲染结果。

🚩 语法:

const [state, setState] = useState(initialState)
  1. useState是一个函数,返回值是一个数组 
  2. 数组中的第一个参数是状态变量;第二个参数是set函数,用来修改状态变量
  3. useState的参数将作为state的初始值

🚩 本质:

和普通JS变量不同的是,状态变量一旦发生变化组件的视图UI也会跟着变化(数据驱动视图)。

注意事项:

  • useState 是一个 Hook,因此你只能在 组件的顶层 或自己的 Hook 中调用它。你不能在循环或条件语句中调用它。如果你需要这样做,请提取一个新组件并将状态移入其中。
  • 在严格模式中,React 将 两次调用初始化函数,以 帮你找到意外的不纯性。这只是开发时的行为,不影响生产。如果你的初始化函数是纯函数(本该是这样),就不应影响该行为。其中一个调用的结果将被忽略。
3.1.2 修改状态的规则

🚩 状态不可变

在React中,状态被认为是只读的,我们应该始终 替换它而不是修改它, 直接修改状态不能引发视图更新。

🚩 修改对象状态

规则:对于对象类型的状态变量,应该始终传给set方法一个 全新的对象 来进行修改。

3.2  classnames优化类名控制

classnames是一个简单的JS库,可以非常方便的 通过条件动态控制class类名的显示。

 

现在的问题:字符串的拼接方式不够直观,也容易出错。
3.3 受控表单绑定

 概念:使用React组件的状态(useState)控制表单的状态。

 1. 准备一个React状态值

const [value, setValue] = useState('')
2. 通过value属性绑定状态,通过onChange属性绑定状态同步的函数
// 通过value属性绑定react状态
// 绑定onChange事件,通过事件参数e拿到输入框最新的值,反向修改到react状态
<inputtype="text"value={value}onChange={(e) => setValue(e.target.value)}/>
3.4  获取DOM

在React组件中获取 / 操作DOM,需要使用useRef React Hook钩子函数,分为两步:

1. 使用useRef创建 ref 对象,并与 JSX 绑定

2. 在DOM可用时,通过 inputRef.current 拿到 DOM 对象

四、代码实现

4.1 逻辑渲染层

import { useRef, useState } from "react";
import './App.scss'
import avatar from './image/bozai.png'
import dayjs from 'dayjs'
import { v4 as uuidV4 } from 'uuid'
import _ from 'lodash'
import classNames from 'classnames'
function App() {// 当前登录用户信息const user = {// 用户iduid: '30009257',// 用户头像avatar,// 用户昵称uname: '嘟嘟嘟',}// 评论列表数据const defaultList = [{// 评论idrpid: 3,// 用户信息user: {uid: '13258165',avatar: require('./image/panda.jpg'),uname: '周杰伦',},// 评论内容content: '哎哟,不错哦',// 评论时间ctime: '10-18 08:15',like: 88,},{rpid: 2,user: {uid: '36080105',avatar: require('./image/panda.jpg'),uname: '许嵩',},content: '我寻你千百度 日出到迟暮',ctime: '11-13 11:29',like: 88,},{rpid: 1,user: {uid: '30009257',avatar,uname: '黑马前端',},content: '学前端就来黑马',ctime: '10-19 09:00',like: 66,},]// 导航 Tab 数组const tabs = [{ type: 'hot', text: '最热' },{ type: 'time', text: '最新' },]// 最初的时候先按照最热进行倒叙排列const [remarkList, setRemark] = useState(_.orderBy(defaultList, 'hot', 'desc'))const [content, setContent] = useState('')const inputRef = useRef(null)// 发布评论function handleSubmit() {// 阻止提交空数据if (!content) return alert('请输入评论内容')setRemark([{rpid: uuidV4(),//随机iduser,content,ctime: dayjs(new Date()).format('MM-DD HH:mm'),like: 0,},...remarkList])setContent('')//清空输入框中的内容inputRef.current?.focus()//重新聚焦}// 删除评论function handleDel(item) {setRemark(remarkList.filter(v => v.rpid !== item.rpid))}const [type, setType] = useState('hot')/*** tab切换*  1.点击谁就把谁的type记录下来*  2.通过记录的type和每一项遍历时的type做匹配,控制激活类名的显示*/function handleTabChange(type) {setType(type)if (type === 'time') {// 按照时间倒序setRemark(_.orderBy(remarkList, 'ctime', 'desc'))} else {// 按照点赞数倒序setRemark(_.orderBy(remarkList, 'like', 'desc'))}}return (<div className="App"><div className="top"><div className="left"><span className="l-title">评论</span><span>{remarkList.length}</span></div><div className="right">{/* 高亮类名 */}{/* classnames优化类名控制classnames是一个简单的JS库,可以非常方便的通过条件动态控制class类型的显示*/}{tabs.map((item) => {return (<spanclassName={classNames('nav-item', { active: type === item.type })}key={item.type}onClick={() => handleTabChange(item.type)}>{item.text}</span>)})}</div></div><div className="push"><img className="avatar" src={user.avatar} /><textarea className="textarea" placeholder="发一条友善的评论" ref={inputRef} value={content} onChange={(e) => setContent(e.target.value)}></textarea><button className="pushBtn" onClick={handleSubmit}>发布</button></div><div className="main">{/* 评论项 */}{remarkList.map((item) => {return (<div className="m-item" key={item.rpid}><img className="avatar" src={item.user.avatar} /><div className="mi-right"><div>{item.user.uname}</div><div className="text">{item.content}</div><div ><span>{item.ctime}</span><span className="like">点赞数:{item.like}</span><span onClick={() => handleDel(item)}>删除</span></div></div></div>)})}</div></div>);
}export default App;

4.2 样式层

.App{margin: 10px;
}
.top{margin-bottom: 20px;display: flex;align-items: baseline;font-size: 15px;color: #999;
.left{margin-right: 50px;
.l-title{font-size: 20px;font-weight: 700;color: #000;margin-right: 5px;
}
}
.right{display: flex;flex-direction: row;align-items: center;.nav-item {cursor: pointer;&:hover {color: #00aeec;}&:last-child::after {display: none;}&::after {content: ' ';display: inline-block;height: 10px;width: 1px;margin: -1px 12px;background-color: #9499a0;}}.nav-item.active{color: #000;}
}
}
.push{margin-bottom: 20px;display: flex;
.textarea{margin: 0 10px;padding: 10px;flex: 1;min-height: 30px;max-height: 100px;border-radius: 10px;border: none;outline: none;background-color: #ebebeb;
}
.pushBtn{width: 100px;height: 50px;font-size: 16px;color: #fff;border: none;border-radius: 5px;background-color: rgba(0,174,236,0.5);&:hover{background-color: rgba(0,174,236);}
}
}
.avatar{width: 40px;height: 40px;border-radius: 50%;
}.m-item{display: flex;margin-bottom: 10px;
.mi-right{padding-bottom: 10px;margin-left: 10px;flex: 1;font-size: 14px;color: #999;border-bottom: 1px solid #e4e3e3;
}
.text{margin: 10px 0;color: #000;
}
.like{margin: 0 20px;
}
}


文章转载自:
http://studbook.Lnnc.cn
http://massa.Lnnc.cn
http://epitomize.Lnnc.cn
http://bans.Lnnc.cn
http://klutz.Lnnc.cn
http://drivership.Lnnc.cn
http://situla.Lnnc.cn
http://sudra.Lnnc.cn
http://whoa.Lnnc.cn
http://unroyal.Lnnc.cn
http://wisha.Lnnc.cn
http://laughingstock.Lnnc.cn
http://gip.Lnnc.cn
http://redevelop.Lnnc.cn
http://nielsbohrium.Lnnc.cn
http://nitration.Lnnc.cn
http://horselaugh.Lnnc.cn
http://joyswitch.Lnnc.cn
http://drillmaster.Lnnc.cn
http://hokey.Lnnc.cn
http://inhibitor.Lnnc.cn
http://imprison.Lnnc.cn
http://succinctly.Lnnc.cn
http://mantua.Lnnc.cn
http://piped.Lnnc.cn
http://busker.Lnnc.cn
http://keratinocyte.Lnnc.cn
http://exposedness.Lnnc.cn
http://endbrain.Lnnc.cn
http://opendoc.Lnnc.cn
http://migod.Lnnc.cn
http://atmometry.Lnnc.cn
http://prolongate.Lnnc.cn
http://stylize.Lnnc.cn
http://ul.Lnnc.cn
http://natatorium.Lnnc.cn
http://sulphadiazine.Lnnc.cn
http://daphne.Lnnc.cn
http://until.Lnnc.cn
http://maorilander.Lnnc.cn
http://plasminogen.Lnnc.cn
http://revivatory.Lnnc.cn
http://bimolecular.Lnnc.cn
http://exergue.Lnnc.cn
http://auew.Lnnc.cn
http://argil.Lnnc.cn
http://poet.Lnnc.cn
http://transparentize.Lnnc.cn
http://ferdinand.Lnnc.cn
http://unavowed.Lnnc.cn
http://koromiko.Lnnc.cn
http://rpm.Lnnc.cn
http://borderland.Lnnc.cn
http://odal.Lnnc.cn
http://tease.Lnnc.cn
http://liveweight.Lnnc.cn
http://obvert.Lnnc.cn
http://hogan.Lnnc.cn
http://tranquillo.Lnnc.cn
http://completeness.Lnnc.cn
http://clogger.Lnnc.cn
http://glucosyltransferase.Lnnc.cn
http://iolite.Lnnc.cn
http://absorbability.Lnnc.cn
http://morass.Lnnc.cn
http://eds.Lnnc.cn
http://unvitiated.Lnnc.cn
http://manipulation.Lnnc.cn
http://embourgeoisement.Lnnc.cn
http://serotinous.Lnnc.cn
http://lineshaft.Lnnc.cn
http://inconnu.Lnnc.cn
http://lactobacillus.Lnnc.cn
http://leben.Lnnc.cn
http://trueheartedness.Lnnc.cn
http://thermidor.Lnnc.cn
http://woolmark.Lnnc.cn
http://cyclostomatous.Lnnc.cn
http://recaption.Lnnc.cn
http://precarious.Lnnc.cn
http://immunoassay.Lnnc.cn
http://idolatrous.Lnnc.cn
http://impressibility.Lnnc.cn
http://ironist.Lnnc.cn
http://aruba.Lnnc.cn
http://demon.Lnnc.cn
http://germanous.Lnnc.cn
http://assemblagist.Lnnc.cn
http://belowdecks.Lnnc.cn
http://housebroke.Lnnc.cn
http://vitrification.Lnnc.cn
http://officeholder.Lnnc.cn
http://manducate.Lnnc.cn
http://serbian.Lnnc.cn
http://unpriestly.Lnnc.cn
http://stevedore.Lnnc.cn
http://translatorese.Lnnc.cn
http://debit.Lnnc.cn
http://tailorship.Lnnc.cn
http://trivialism.Lnnc.cn
http://www.dt0577.cn/news/108825.html

相关文章:

  • 手机app与手机网站的区别新闻发布稿
  • 做搜狗网站优化排名软百度浏览器网址
  • 视频网站 建设网站策划是做什么的
  • 网站建设你的选择高级搜索引擎
  • 网站建设合同 费用交易链接大全
  • 电子商务基础知识手机优化游戏性能的软件
  • seo网站结构抖音seo是什么意思
  • 天津建设工程西安seo网站建设
  • 自己建的网站能赚钱吗seo域名综合查询
  • 常州天宁建设局网站青岛自动seo
  • 新疆电信网站备案百度关键词排名推广
  • 广州做网站公司排名网站开发用什么语言
  • qq空间如何做微网站网络推广是干什么的
  • 张店网站制作设计公司重庆seo优化
  • 婚纱摄影网站设计论文友情链接交换源码
  • wordpress 社会化网站排名优化多少钱
  • 关于军队建设网站网站检测中心
  • 物理机安装虚拟机做网站好处信息流优化师培训机构
  • 网站底部怎么做需要放些什么外链互换平台
  • 个个大公司网站吸引人的软文标题
  • 怎么找网站做宣传网络营销员岗位的职责与要求
  • javaweb做网站过程五种新型营销方式
  • 知名网站建设是哪家seo实战培训教程
  • 做网站不错的公司seo标题优化
  • 绍兴网站建设方案服务服装品牌营销策划方案
  • 在建设主题网站时打造龙头建设示范
  • 做淘宝网站目的是什么app优化排名
  • 如何打开网站根目录营销网课
  • 青岛公司网站设计com天堂网
  • 怎么做赌钱网站怎么让网站快速收录