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.

For the most part it is fine to skip the 3rd argument of addEventListener but some event types do not bubble - which creates a dilemma when using event delegation (for example, to avoid passing callbacks down the component tree).

I’ve created some very simple demos here:

All synthetic events in React bubble (or at least appear to do so) so this can create some incompatibilities when switching to Preact.

UPDATE: React explicitly supports capturing by appending Capture to the attribute, E.G. onClick would become onClickCapture https://facebook.github.io/react/docs/events.html#supported-events and certain events are set to capture automatically (inc. focus and blur).

I’m not fully aware of the ramifications that changing this line would be. Perhaps it may be preferable to check the event type against a capturing whitelist.

I’m happy to help implement any changes, even if it’s just a note in the docs 😄

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
developitcommented, Aug 16, 2016

I think that makes more sense - I was worried about swapping to capturing phase for things like click, because there are definitely people invoking stopPropagation() on those types of events, expecting them not to bubble (up). The events in the whitelist you linked to aren’t bubbling events anyway, so I think it’d be alright to silently switch those over to useCapture.

Another option (maybe less pleasant) would be to manually bubble the non-bubbling events - Preact already hooks all events, and we could do something like this? (though I worry this is venturing into virtual eventing territory)

let whitelist = ['change', 'input', 'etc'];

function eventProxy(e, node) {
  e = options.event && options.event(e) || e;
  node = node || this;
  let fn = node._listeners[e.type];
  if ((!fn || fn(e)!==false) && !e.propagationStopped) {
    eventProxy(e, node.parentNode);
  }
  return ret;
}
0reactions
i-like-robotscommented, Aug 16, 2016

I’ve asked around and had some input as to why this may be a bad idea in some cases, and why a whitelist may be preferable.

If all listeners are triggered at the capture phase then calling preventDefault() within the callback of an inner node listener will not stop it from reaching an outer node listener. 😢

However, it can be sensible (preferable?) to use capture for some events, like those previously mentioned… and perhaps a few more.

(Thanks to @matthew-andrews for refresher course!)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bubbling and capturing
A bubbling event goes from the target element straight up. Normally it goes upwards till <html> , and then to document object, and...
Read more >
What is event capturing in JavaScript?
Event capturing is one of two ways to do event propagation in the HTML DOM. In event capturing, an event propagates from the...
Read more >
What is Event bubbling and Event Capturing in JavaScript
Event capturing means propagation of event is done from ancestor elements to child element in the DOM while event bubbling means propagation is ......
Read more >
What is Event Capturing in JavaScript?
Event capturing is the event starts from top element to the target element. It is the opposite of Event bubbling, which starts from...
Read more >
What is event bubbling and capturing?
Event bubbling and capturing are two ways of event propagation in the HTML DOM API, when an event occurs in an element inside...
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