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

149
Views
How to only expand one div out of a map?

I'm trying to map through an API and make each of its objects have expandable data. However, when I expand a div for one object, instead of only expanding that particular div it expands all of my objects. How do i made it so that it only expands that one object data? Here is my code:

    export default function StudentsList({ students }) {
  const [isCollapsed, setIsCollapsed] = useState(true);


  const handleCollapse = () => {
    setIsCollapsed((prev) => !prev);
  };


  return (
    <div className={studentsBox}>

      {students.map((student) => (
          <div className={card} key={student.id}>
             <div
                className={plus}
                onClick={handleCollapse}
                
              >
                {isCollapsed ? '+' : '-'}
              </div>
                
                <div className={isCollapsed ? collapse : ''}>
                  <p>Test 1: &nbsp;&nbsp; {student.grades[0]}%</p>
                  <p>Test 2: &nbsp;&nbsp; {student.grades[1]}%</p>
                  <p>Test 3: &nbsp;&nbsp; {student.grades[2]}%</p>
                  <p>Test 4: &nbsp;&nbsp; {student.grades[3]}%</p>
                  <p>Test 5: &nbsp;&nbsp; {student.grades[4]}%</p>
                  <p>Test 6: &nbsp;&nbsp; {student.grades[5]}%</p>
                  <p>Test 7: &nbsp;&nbsp; {student.grades[6]}%</p>
                  <p>Test 8: &nbsp;&nbsp; {student.grades[7]}%</p>
                </div>

              </div>
          </div>
        ))}
      );
   }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can do something like this

 export default function StudentsList({ students }) {
  const [collapsed, setCollapsed] = useState(null);


  const handleCollapse = (id) => {
    setCollapsed(prev => prev === id? null : id);
  };


  return (
    <div className={studentsBox}>

      {students.map((student) => (
          <div className={card} key={student.id}>
             <div
                className={plus}
                onClick={() => handleCollapse(student.id)}
                onMouseMove={() => setId(`${student.id}`)}
              >
                {collapsed === student.id ? '+' : '-'}
              </div>
                
                <div className={collapsed === student.id ? collapse : ''}>
                  <p>Test 1: &nbsp;&nbsp; {student.grades[0]}%</p>
                  <p>Test 2: &nbsp;&nbsp; {student.grades[1]}%</p>
                  <p>Test 3: &nbsp;&nbsp; {student.grades[2]}%</p>
                  <p>Test 4: &nbsp;&nbsp; {student.grades[3]}%</p>
                  <p>Test 5: &nbsp;&nbsp; {student.grades[4]}%</p>
                  <p>Test 6: &nbsp;&nbsp; {student.grades[5]}%</p>
                  <p>Test 7: &nbsp;&nbsp; {student.grades[6]}%</p>
                  <p>Test 8: &nbsp;&nbsp; {student.grades[7]}%</p>
                </div>

              </div>
          </div>
        ))}
      );
   }

about 4 years ago · Juan Pablo Isaza Report

0

First you declare the selectedId

const [selectedId, setSelectedId] = useState(null);

Then set onClick to setSelectedId

onClick={()=>setSelectedId(student.id)}

This way you can change the className base on the selectedId

<div className={student.id !== selectedId ? collapse : ''}>
  <p>Test 1: &nbsp;&nbsp; {student.grades[0]}%</p>
  <p>Test 2: &nbsp;&nbsp; {student.grades[1]}%</p>
....
</div>
about 4 years ago · Juan Pablo Isaza Report

0

The beauty of React is to make use of lots of components. Use additional components to add some structure to your presentation of your data.

  • StudentList (shows a list of...)
    • Student (which, in turn, shows a list of...)
      • StudentGrade

Each student can easily be collapsed independently from the list of students.

function StudentGrade({ grade, index }) {
  return <p>Test {index}: &nbsp;&nbsp; {grade}</p>
}

function Student({ student }) {
  const [isCollapsed, setIsCollapsed] = React.useState(true);

  const handleCollapse = () => {
    setIsCollapsed((prev) => !prev);
  };

  return (
    <div>
      <div onClick={handleCollapse}>
        {isCollapsed ? '-' : '+'}
        <span>{student.name}</span>
      </div>
      {!isCollapsed?null:(
        <div>
          {student.grades.map((grade, index) => (
            <StudentGrade key={index} grade={grade} index={index}/>
          ))}
        </div>
      )}
    </div>
  )
}

function StudentsList({ students }) {
  return (
    <div>
      {students.map((student) => (
        <Student key={student.id} student={student} />
      ))}
    </div>
  );
}

const students = [
  { id: 1, name: 'Ann', grades: ['A', 'B'] },
  { id: 2, name: 'Bob', grades: ['B', 'C'] },
  { id: 3, name: 'Charlie', grades: ['C', 'D'] },
]
ReactDOM.render(<StudentsList students={students} />, document.getElementById('root'));
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script>
</script>
<div id="root"></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!