Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are React ...Capture events?

Tags:

dom

reactjs

There is a duplicate for each event on react audio and video tags, for example it has onCanPlayThrough property and onCanPlayThroughCapture as well. Behaviour for both of them in the browser is completely the same.

Do we need to use ...Capture events instead of standard events? What is the main idea for them? Where can I find information about them.

like image 336
Dmitriy Kovalenko Avatar asked Feb 24 '17 13:02

Dmitriy Kovalenko


People also ask

What is event capturing React?

React onClickCapture is an event handler that gets triggered whenever we click on an element. like onclick, but the difference is that onClickCapture acts in the capture phase whereas onClick acts in the bubbling phase i.e. phases of an event.

What is event capture 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 outermost element to the target element. It is the opposite of event bubbling, where events propagate outwards from the target to the outer elements.

What is the difference between event bubbling and event capturing?

With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements. With capturing, the event is first captured by the outermost element and propagated to the inner elements.


1 Answers

This is covered in the React documentation here, though it's easy to miss:

The event handlers below are triggered by an event in the bubbling phase. To register an event handler for the capture phase, append Capture to the event name; for example, instead of using onClick, you would use onClickCapture to handle the click event in the capture phase.

DOM events have multiple phases (see diagram below), which are (in order):

  • Capture
  • Target
  • Bubbling

Normally we use target or bubbling (addEventListener's false third param [the default]), but there are rare occasions where you want to hook into the capture phase instead (third param = true). The ...Capture handlers let you do that.

event flow showing capture from window to target element, then target at element, then bubbling from element back up to window

like image 115
T.J. Crowder Avatar answered Sep 28 '22 23:09

T.J. Crowder