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

158
Vistas
React - accessing DOM value of parent element

I have a React component that renders a grid like this :

    for (let i=0; i<items.length; i++) {
        if (filterItem(items[i])) {
          rows.push(
            <div className="row" key={items[i]._id} id={items[i]._id} onClick={props.onClick}>
              {props.db !== "videos" && <div className="centered">{items[i].level}</div>}
              {props.db === "questions" && <div className="centered left">{items[i].question}</div>}
              {props.db !== "questions" &&  <div className="centered left">{items[i].instructions}</div>}
              <div className="centered" onClick={props.delete}><FontAwesomeIcon icon={ faTrashAlt }/></div>
            </div>
          );
        }
    }

I display some items from different databases and depending on the database the fields are not the same. I need to implement onClick handler which will either preview a video (in case of videos database is displayed) or get the id value of the grid row (in case of any other database is displayed)

My current code looks like this:

    handleClick(e) {
        
        if (this.state.database === "videos") {
          this.setState(state => ({
            videoPreview: this.getDbItem(e.target.innerText).link
          }))
        }

        else if (e.target.parentElement.id) {
          this.editMode(e.target.parentElement.id) //get id of the row div
    
        }
    
    }

and it works, but seems wrong...

Is there a better way to do this?

about 4 years ago · Juan Pablo Isaza
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