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

239
Visualizações
Don't open href while an onClick is specified (in react js)

Our <a> tag opens a custom pdf viewer in our application by providing a function to the onClick property.

We want to provide a href for accessibility, so users can rightclick -> open in new tab, control+click or what else they fancy.

However, when the user clicks the <a> tag, we don't want to actually open the link, instead our onClick function should handle the event.

currently we have somthing like this:

render(){
  return (<a href="www.stackoverflow.com" onClick={(e)=>this.linkClick(e)} >click here</a>)
}

linkClick(e) {
   e.stopPropagation(); // does not stop the link from opening
   console.log("link clicked");
   return false; // does not stop the link from opening
}

In plain js this can be done by returning false on the onClick function. We've also tried adding stopPropagation() to no avail. Some react users add href="javascript: void(0)" while this does disable the link, it does not meet our accessibility needs.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use preventDefault. Returning false has been deprecated since React 0.12.

linkClick(e) {
   e.preventDefault();
   console.log("link clicked");
}
over 4 years ago · Santiago Trujillo Relatório
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