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

142
Visualizações
Button Click inside anchor tag not working in React Js Component

Button onClick not executing the triggerThis function because it is wrapped with react-router-dom Link component

Expected behaviour: Card click should navigate to /app/url and on button click the url navigation should be prevented and should run triggerThis function.

I want onClick button prevent navigation. but when i click outside button the navigation should work .. triggerThis is a function that just console.log("hello")

<Card>
    <Link to={`/app/url`}>
       <h1>Title</h1>
       <p>Paragraph</p>
       <button onClick={triggerThis}></button>
    </Link>
   
 </Card>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You really shouldn't place interactive elements within other interactive elements, but if you want to allow the button to be independently clickable and not trigger the navigation of the wrapping Link component then you should stop the click event propagation. This prevents the click event from propagating further up the DOMtree to the Link element, thus preventing the navigation action from occurring.

Example:

const clickHandler = e => {
  e.stopPropagation();
  ...
};

...

<Card>
  <Link to="/app/url">
    <h1>Title</h1>
    <p>Paragraph</p>
    <button onClick={clickHandler}>
      ...
    </button>
  </Link>
</Card>
about 4 years ago · Juan Pablo Isaza 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