0

I have comments that i loop through: enter image description here

When i click on the three dots i want there to pop a little div out with the text "repport comment".

But when i click on one of buttons they all open :

enter image description here

import { FaEllipsisV } from "react-icons/fa";
import "./styles.css";
import React from "react";

const data = [
  {
    postId: 1,
    id: 1,
    name: "id labore ex et quam laborum",
    email: "[email protected]",
    body:
      "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
  },
  {
    postId: 1,
    id: 2,
    name: "quo vero reiciendis velit similique earum",
    email: "[email protected]",
    body:
      "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
  },
  {
    postId: 1,
    id: 3,
    name: "odio adipisci rerum aut animi",
    email: "[email protected]",
    body:
      "quia molestiae reprehenderit quasi aspernatur\naut expedita occaecati aliquam eveniet laudantium\nomnis quibusdam delectus saepe quia accusamus maiores nam est\ncum et ducimus et vero voluptates excepturi deleniti ratione"
  },
  {
    postId: 1,
    id: 4,
    name: "alias odio sit",
    email: "[email protected]",
    body:
      "non et atque\noccaecati deserunt quas accusantium unde odit nobis qui voluptatem\nquia voluptas consequuntur itaque dolor\net qui rerum deleniti ut occaecati"
  },
  {
    postId: 1,
    id: 5,
    name: "vero eaque aliquid doloribus et culpa",
    email: "[email protected]",
    body:
      "harum non quasi et ratione\ntempore iure ex voluptates in ratione\nharum architecto fugit inventore cupiditate\nvoluptates magni quo et"
  }
];

export default function App() {
  const [showOptions, setShowOptions] = React.useState(false);

  return (
    <div className="App">
      {data.map((comment, index) => (
        <div key={index} className="comment-container">
          {comment.name}
          <button onClick={() => setShowOptions(!showOptions)}>
            <FaEllipsisV />
          </button>

          {showOptions ? (
            <div className="options">Report this comment</div>
          ) : null}
        </div>
      ))}
    </div>
  );
}

https://codesandbox.io/s/elated-roentgen-fbjr7?file=/src/App.js:0-2121

Example of what i'd like :

enter image description here

1 Answer 1

1

You can do something like this:

export default function App() {
  const [showOptions, setShowOptions] = React.useState({ id: null, status: false });

  return (
    <div className="App">
      {data.map((comment, index) => (
        <div key={index} className="comment-container">
          {comment.name}
          <button onClick={() => setShowOptions({ id: comment.id, status: !showOptions.status })}>
            <FaEllipsisV />
          </button>

          {showOptions.status && comment.id === showOptions.id ? (
            <div className="options">Report this comment</div>
          ) : null}
        </div>
      ))}
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.