0

I cannot figure out why All of the defitions show on the screen. My goal is to display all 4 buttons(terms), and only show the definition for that term. As it is now it keeps showing all the definitions?? Something about the way I am accessing the Data? There are multiple objects in SlideData array, im just showing one because its the only one with Terms.

const SlideData = [
   {
    index: 7,
    title: "Key Terms (1 of 3)",
    content: [
      {
        type: "terms",
        terms: [
          {
            name: "Business Intelligence",
            definition:
              "definition 1",
          },
          {
            name: "DCPS",
            definition:
              "definition 2",
          },
          {
            name: "Civilian Personnel System:",
            definition:
              "definition 3",
          },
          {
            name: "Defense: ",
            definition: " definition 4",
          },
        ],
      },
    ],
  },
];

export default SlideData;
const TextSlide = (props) => {
  //STATE MANAGEMENT//
  const [clickedTerm, setClickedTerm] = useState(0);
  const [showClicked, setShowClicked] = useState(true);

  //CHANGE STATE//
  const handleClick = (i) => {
    console.log(i);
    setClickedTerm(i);
    setShowClicked(true);
  };

  //paragraph
  function Paragraph({ text }) {
    return <p>{text}</p>;
  }
 
  //TERMS RENDER
  function Terms({ title, terms }) {
    return (
      <>
        {title && <p>{title}</p>}
        {terms.map((item, index) => (
          <div">
            <div">
              {showClicked && (
                <div>
                  <h3>{terms[clickedTerm].name}</h3>
                  <div>{terms[clickedTerm].definition}</div>
                </div>
              )}
            </div>
            <button key={index} onClick={() => handleClick(index)}>
              {item.name}
            </button>
          </div>
        ))}
      </>
    );
  }


  const elements = {
    paragraph: Paragraph,
    terms: Terms,
  };

  return (
    <>
      <div className="slide">
        <div className="standard-grid">
          <span className="slide-title title">{props.title}</span>
          <div className="content">
            {props.content.map((item, i) => {
              const Comp = elements[item.type];
              return <Comp key={i} {...item} />;
            })}
          </div>
        </div>
      </div>
    </>
  );
};


export default TextSlide;
1
  • It's not a good idea to define components inside another functional component (e.g. Paragraph and Terms). It will cause them to unmount and remount every render cycle Commented Aug 5, 2021 at 19:02

1 Answer 1

1

You're rendering the definition div within the map() callback.

Try this:

  function Terms({ title, terms }) {
    return (
      <>
        {title && <p>{title}</p>}
        <div>
          {showClicked && (
            <div>
              <h3>{terms[clickedTerm].name}</h3>
              <div>{terms[clickedTerm].definition}</div>
            </div>
          )}
        </div>
        {terms.map((item, index) => (
          <div>
            <button key={index} onClick={() => handleClick(index)}>
              {item.name}
            </button>
          </div>
        ))}
      </>
    );
  }

As an aside, it's generally bad practice to use the index as the key. In this case, I'd use item.name. The key also needs to be placed at the topmost element, so the div rather than the button.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! I ll change those indexes too I couldn't see what I was doing wrong!

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.