import { useRef, useState } from 'react'
import _ from 'lodash'
import classnames from 'classnames'
import { v4 as uuidV4 } from 'uuid'
import dayjs from 'dayjs'
const App = () => {
const [commentList,setCommentList]=useState(_.orderBy(list,'like','desc'));
const handleTabChange = (type) => {
setType(type)
if(type == 'hot') {
setCommentList(_.orderBy(commentList,'like','desc'))
}else {
setCommentList(_.orderBy(commentList,'ctime','desc'))
}
}
const [content, setContent] = useState('')
const inputRef = useRef(null)
setCommentList([
...commentList,
{
rpid: uuidV4(),
user: {
uid: '30009257',
avatar:avatar,
uname: '黑马前端',
},
content: content,
ctime: dayjs(new Date()).format('MM-DD HH:mm'),
like: 66,
}
])
setContent('')
inputRef.current.focus()
}
return (
<div className="app">
//发表评论,点击发布后
<textarea
className="reply-box-textarea"
placeholder="发一条友善的评论"
ref={inputRef}
value={content}
onChange={(e) => setContent(e.target.value)}
/>
<div className="reply-box-send">
<div className="send-text" onClick={handlePublish}>发布</div>
</div>
//切换tab
<ul>
{tabs.map(item =>
<span key={item.type}
onClick={() => handleTabChange(item.type)}
className={`nav-item ${type === item.type && 'active'}`}>
{item.tabName}
</span>
)}
//如果用classnames插件就写成,classnames方法里面第一个参数是静态class名,第二个参数是动态class,key是active即动态的class名称,value是条件:
//className={classnames('nav-item',{active:type === item.type})}
</ul>
</div>
)}