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

157
Vistas
How can I give different functionality to a button that was created in a map function in React?

I'm using .slice to display only one employee's information by clicking on a button, but since I'm mapping the button to each employee the functionality is exactly the same for each employee, and can't think of a way for each employee's button to perform .slice at the correct place.

      <Modal
        isOpen={modalIsOpen}
        onAfterOpen={afterOpenModal}
        onRequestClose={closeModal}
        style={customStyles}
        contentLabel="Example Modal"
      >
        <h2 ref={(_subtitle) => (subtitle = _subtitle)}>More Info</h2>
        <button onClick={closeModal}>close</button>
        {employees.slice(0, 1).map(info => (
          <>
          <p>{info.firstName}</p>
          <p>{info.lastName}</p>
          <p>{info.email}</p>
          <p>{info.phone}</p>
          <p>{info.address.streetAddress}, {info.address.city} {info.address.state} {info.address.zipCode}</p>
          <p>{info.bio}</p>
          </>
        ))}
      </Modal>

Each employee appears in a table and has this More Info button. There are 10 employees and each button opens the modal and lists the info for the employee at .slice(0,1), but I need each button to open at the correct place, (i.e. .slice(1, -8) will always display the info for the second employee)

                    <td>
                        <button onClick={() => setEmployeeToUpdate(id)}>Update</button>
                        <button onClick={() => moreInfo(id)}>More Info</button>
                        <button onClick={() => deleteEmployee(id)}>Delete</button>        
                    </td>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Are you just trying to get the selected employee info into the modal? If so, you could track the selected employee in state and show the modal when one is selected. To close the modal simply clear the selection:

function EmployeeList ({employees}) {
   const [selectedEmployee, setSelectedEmployee] = useState();

   return (
     <>
        <ul>
          { employees.map(e => (
              <li key={e.id} onClick={() => setSelectedEmployee(e)}>
                {e.firstName}
              <li>
            )
          )}
        </ul>
        <Modal isOpen={selectedEmployee}>
          <button onClick={() => setSelectedEmployee(null)}>close</button>
          <p>{selectedEmployee?.firstName}</p>
          <p>{selectedEmployee?.lastName}</p>
          <p>{selectedEmployee?.email}</p>
        </Modal>
     </>
   )
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

@Ray gave a good answer for using state to share the data.

I would aim to place the map somewhere that allows using the data in both places rather than storing a copy in state.

employees.map(employee => <>
  <td>
      <button onClick={() => setEmployeeToUpdate(employee.id)}>Update</button>
      <button onClick={() => showModel()}>More Info</button>
      <button onClick={() => deleteEmployee(employee.id)}>Delete</button>        
  </td>
  <InfoModel info={employee}>
  </>
})
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