Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!