Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda