一键反应所有评论选项

发布于 2025-01-18 09:57:43 字数 1785 浏览 1 评论 0原文

我有这段代码,我循环 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2025-01-25 09:57:44

存储用于控制的点击注释ID:

const [shownCommentId, setShownCommentId] = useState('');


{user._id === comment.userId ? (
      <div
         className="commentOptionsMark"
         onClick={() => setShownCommentId(comment._id)}
      > - </div>
) : null}

然后在地图中控制ID:

{shownCommentId === comment._id ? (
      <div className="commentOptions">
          <div className="deleteComment">Delete</div>
          <div className="editComment">Edit</div>
      </div>
 ) : null}

store the clicked comments id for controlling:

const [shownCommentId, setShownCommentId] = useState('');


{user._id === comment.userId ? (
      <div
         className="commentOptionsMark"
         onClick={() => setShownCommentId(comment._id)}
      > - </div>
) : null}

then control the id in your map:

{shownCommentId === comment._id ? (
      <div className="commentOptions">
          <div className="deleteComment">Delete</div>
          <div className="editComment">Edit</div>
      </div>
 ) : null}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文