Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
React Handling component onClick (one opens, but another closes)

I'm having issues with finding a solution or correct direction for my problem. At the moment once you click on -> className="List-section", it will show the information in -> className="Paragraph-container", but there are 4 sections of it and I want to make that if one is open, another closes. Because now I can open all off then, but it has to be opened one at a time only. If it helps, here's the link to the prototype: https://www.figma.com/proto/ooya7hVTx4BvdwQQFPXZcj/CCT-Lab-task?node-id=5%3A711&scaling=min-zoom&page-id=0%3A1&starting-point-node-id=5%3A509

Thanks for all the help in advance!

function ListItem({ listNumber, listData }) {
  const [isShownOne, setIsShownOne] = useState(false);

  return (
    <div className="List">
      <div className="List-section" onClick={() => setIsShownOne(!isShownOne)}
      >
        <div className="Section-item">{listNumber}</div>
        <p className="Section-title">{listData.title}</p>
      </div>
      {isShownOne && (
        <div className="List-paragraph">
          {listData.items.map((paragraphItem, index) => (
            <div className="Paragraph-container" key={index}>
              <CheckedIcon  />
              <p className="Paragraph-item">{paragraphItem}</p>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think that the structure that you have here is not optimal for this behavior that you're looking for.

I would suggest doing something like:

function ListItem({ listNumber, listData }) {
  const [currentSection, setCurrentSection] = useState(0);
  
  const totalSections = listData.items.length;
  
  const handleOpenSection = () => {
     const nextSection = currentSection === totalSections ? 0 : currentSection + 1;
     setCurrentSection(nextSection);
  }

  return (
    <div className="List">
      <div className="List-section" onClick={handleOpenSection}
      >
        <div className="Section-item">{listNumber}</div>
        <p className="Section-title">{listData.title}</p>
      </div>
        <div className="List-paragraph">
          {listData.items.map((paragraphItem, index) => (
            {currentSection === index && <div className="Paragraph-container" key={index}>
              <CheckedIcon  />
              <p className="Paragraph-item">{paragraphItem}</p>
            </div>
            }
          ))}
        </div>
    </div>
  );
}

As you're handling 4 sections, it would be impossible to implement this behavior using a boolean, so this suggestion uses the position of each section in the items array to see which one is open at the time.

The handleOpenSection function is just a suggestion that seemed fit since you wouldn't want to have a currentSection greater than the total sections that you have. Also this code would show the first section in the first render, which is something you might not want to happen!

about 4 years ago · Juan Pablo Isaza Report

0

This is where I'm getting data from and passing logic to the component.

function App() {
  const data = [
    {
      title: "Build test task",
      items: [
        "Create repositor",
        "Implement designs",
        "Implement functionality",
      ],
    },
    {
      title: "Submit your test task",
      items: [
        "Open email client",
        "Sent link information to careers@cornercasetech.com",
      ],
    },
    {
      title: "Participate in tech interview",
      items: ["Talk with HR", "Talk with Tech team"],
    },
    {
      title: "Reciece anster",
      items: ["Receive answers", "Start your IT career"],
    },
  ];

  return (
    <div className="App">
      <header className="App-header">
        <h1>CCT Lab Process</h1>
      </header>
      <div className="App-content">
      {data.map((dataObject, index) => (
        <ListItem key={index} listData={dataObject} listNumber={index + 1} />
      ))}
      </div>

    </div>
  );
}

And here's where I suppose to be handling the logic for the sections, that if you open one, the previously opened closes and so on.

function ListItem({ listNumber, listData }) {
  const [isShownOne, setIsShownOne] = useState(false);

  return (
    <div className="List">
      <div className="List-section" onClick={() => setIsShownOne(!isShownOne)}
      >
        <div className="Section-item">{listNumber}</div>
        <p className="Section-title">{listData.title}</p>
      </div>
      {isShownOne && (
        <div className="List-paragraph">
          {listData.items.map((paragraphItem, index) => (
            <div className="Paragraph-container" key={index}>
              <CheckedIcon  />
              <p className="Paragraph-item">{paragraphItem}</p>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!