Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda