skip to Main Content

I’m trying to add a button inside the component which is link ()as a whole already. But I want my button to be unlinked but also positioned inside that linked component.

I mean when I click on that button it should do what function I assigned using (onClick)but not redirecting to the component Link.

I’m working with SPA using React.

return(

{name}

{cuisine}

Rating: {rating}

Delivery Time: (deliverytime+”minutes”}

{costfortwo}

Add/button

2

Answers


  1. The onClick handler of your Button receives an Event object as first parameter, try to call the preventDefault method on this object.

    const myButtonClickHandler = (e) => {
      e.preventDefault();
      // ... the rest of your handler method
    }
    
    Login or Signup to reply.
  2. you can use stopPropogation function to achieve that purpose.

    <Link href="/link-location">
       <button onClick={(e)=>{
          e.stopPropogation()
          //button onClick functionality goes here
       }}>button text</button>
    </link>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search