question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not prevent a child onClick event from the parent's onMouseUp event

See original GitHub issue

img_20151208_172309

I do click down, mouse move, and click up (all this events apply within element area). Events onMouseDown, onMouseMove, onMouseUp defined on the parent element SwipePanel. So I try prevent onClick event on Element component such way:

class SwipePanel extends React.Component {
  handleMouseUp(event) {
    if(event.nativeEvent) {
      event.nativeEvent.preventDefault();
      event.nativeEvent.stopPropagation();
    }
    event.preventDefault();
    event.stopPropagation();
    console.log('SwipePanel onMouseUp');
  }
  ...
}
class Element extends React.Component {
  handleMouseClick(event) {
    console.log('Element onClick');
  }
  ...
}

Via console.log I see

SwipePanel onMouseUp
Element onClick

So I need prevent childs’s onClick event from parent component via onMouseUp. I am using React v0.14.3

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
heineiuocommented, Apr 25, 2016

@nodkz You can use onClickCapture in parent component and stopPropagation if event target is not child component.

https://facebook.github.io/react/docs/events.html#supported-events

18reactions
dawsbotcommented, Apr 16, 2019

Another method that I’ve found to be quite straightforward for child onClick ignoring. Give the parent a unique ID. Perhaps "swipe-panel" in your example. Then In the parent’s onClick:

handleMouseClick = event => {
  const target = event.target;
  if (target.id !== 'swipe-panel') {
    return ; // child was clicked, ignore onClick
  }
  
  // rest of click logic here

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent child's onclick after mouseup on parent?
Is there any way to prevent the click event from happening in the child, after the mouseup event has been triggered in the...
Read more >
SyntheticEvent - React
This reference guide documents the SyntheticEvent wrapper that forms part of React's Event System. See the Handling Events guide to learn more.
Read more >
ASP.NET Core Blazor event handling - Microsoft Learn
An onclick event occurring in the child component is a common use case. To expose events across components, use an EventCallback. A parent...
Read more >
Element: click event - Web APIs | MDN
An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released...
Read more >
Handling Events :: Eloquent JavaScript
<button>Click me</button> <p>No handler here. ... For most event types, handlers registered on nodes with children will also receive events that happen in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found