Dreams
Dreams

Reputation: 8506

How to only trigger one onClick function when inside a component also with onClick event?

Below is the sample code....

<div onClick={this.handeAAAClick}>
  <img onClick={this.handleLinkClick}/>
</div>

When I click the div and it will trigger handleAAAClick event, however, when I only click on img, and it will trigger both click event, what I want is only to trigger handleLinkClick.

How should I do that?

Upvotes: 2

Views: 251

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68635

The handleLinkClick will get a parameter event.On this parameter there is a stopPropagation method.You need to call it.

function handleLinkClick(e){
   e.stopPropagation();
}

Upvotes: 4

Related Questions