一键反应所有评论选项
我有这段代码,我循环 post
数组来为
const [commentOptions, setCommentOptions] = useState(false);
return(
...
{post.comments.map((comment) => (
<div className="comments" key={comment._id}>
<div className="commentUserImageAndUsername">
<img
className="commentUserImg"
src={
comment.userId === users._id
? users.profileImg || `${PF}/profile.jpg`
: null
}
alt="comment user "
/>
{comment.userId === users._id ? users.firstname : null}
</div>
<div className="commentBody">{comment.body}</div>
{user._id === comment.userId ? (
<div
className="commentOptionsMark"
onClick={() => setCommentOptions(!commentOptions)}
> - </div>
) : null}
{commentOptions ? (
<div className="commentOptions">
<div className="deleteComment">Delete</div>
<div className="editComment">Edit</div>
</div>
) : null}
</div>
))}
...
)
每个 comment
提取 comments
我有 hyphen
标记-
,如果我点击它,它会将commentOptions
状态设置为true,因此会出现commentOptions div
,其中包含2个选项删除评论
并编辑comment
,这样做的问题是,如果我点击一条评论的连字符标记,则每条评论都会出现 commentOptions div
,就像有 5 条评论一样评论,每个评论都有自己的选项,我只需点击一下即可控制每个评论的 commentOptions div
的外观,这不是我想要的,我想为每个评论显示一个 div 连字符 -
点击时,不是全部就像每条评论本身一样,如何解决这个问题?
i have this code where I'm looping post
array to extract comments
const [commentOptions, setCommentOptions] = useState(false);
return(
...
{post.comments.map((comment) => (
<div className="comments" key={comment._id}>
<div className="commentUserImageAndUsername">
<img
className="commentUserImg"
src={
comment.userId === users._id
? users.profileImg || `${PF}/profile.jpg`
: null
}
alt="comment user "
/>
{comment.userId === users._id ? users.firstname : null}
</div>
<div className="commentBody">{comment.body}</div>
{user._id === comment.userId ? (
<div
className="commentOptionsMark"
onClick={() => setCommentOptions(!commentOptions)}
> - </div>
) : null}
{commentOptions ? (
<div className="commentOptions">
<div className="deleteComment">Delete</div>
<div className="editComment">Edit</div>
</div>
) : null}
</div>
))}
...
)
for every comment
i have the hyphen
mark -
, if i click it, it will set the commentOptions
state to true, therefore the commentOptions div
will appear, which contains 2 options delete comment
and edit comment
, the problem with this is that if i click the hyphen mark
for one comment, the commentOptions div
will appear for every comment, like if there are 5 comments and every comment has its own options, i can control the appearance of commentOptions div
for every comment by just one click, this is not what i want, i want to show one div for every comment hyphen -
on click, not all of them at once, like every comment on its own, how to fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存储用于控制的点击注释ID:
然后在地图中控制ID:
store the clicked comments id for controlling:
then control the id in your map: