Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In React, what is the difference between onKeyUp and onKeyUpCapture (and onKeyDown/Capture)?

I searched for documentation and questions for this but was surprised to not be able to find one.

In the React index.d.ts file it shows:

// Keyboard Events
onKeyDown?: KeyboardEventHandler<T>;
onKeyDownCapture?: KeyboardEventHandler<T>;
onKeyPress?: KeyboardEventHandler<T>;
onKeyPressCapture?: KeyboardEventHandler<T>;
onKeyUp?: KeyboardEventHandler<T>;
onKeyUpCapture?: KeyboardEventHandler<T>;

And I wanted to use onKeyUp but then I noticed the two versions. What is the difference between this and onKeyUpCapture, and what are the benefits of one over the other? When would I use each?

like image 407
Mayron Avatar asked Jan 22 '20 05:01

Mayron


People also ask

What is the difference between onKeyDown and onKeyUp?

The onKeyDown event is triggered when the user presses a key. The onKeyUp event is triggered when the user releases a key.

What is the difference between Keyup and Keydown in angular?

The keydown event is triggered first when user presses a key. The keyup event is triggered last when user releases a key. In between, the keypress event is triggered.

What is onKeyDown event?

The onkeydown event occurs when the user is pressing a key (on the keyboard).


1 Answers

Event handlers in react passes an instances of SyntheticEvent, a cross-browser wrapper around the browser’s native event. The term capture is not related to React. In fact it's a concept from DOM HTML Event.

The are 3 general phases of event propagation DOM Events:

The capture phase: The event object propagates through the target’s ancestors from the Window to the target’s parent. (the event goes down to the element). You barely might want to use it in real code.

The target phase: The event object arrives at the event object’s event target. This phase is also known as the at-target phase. If the event type indicates that the event doesn’t bubble, then the event object will halt after completion of this phase.

The bubble phase: The event object propagates through the target’s ancestors in reverse order, starting with the target’s parent and ending with the Window. This phase is also known as the bubbling phase.

Event objects are dispatched to an event target. But before dispatch can begin, the event object’s propagation path must first be determined.

The propagation path is an ordered list of current event targets through which the event passes. This propagation path reflects the hierarchical tree structure of the document. The last item in the list is the event target, and the preceding items in the list are referred to as the target’s ancestors, with the immediately preceding item as the target’s parent.

Here is an example of DOM event flow clicking on <td>

As you can see in this picture, it shows the capture phase that starts from the window going through the ancestors chain down to the element target. Then when it gets to the target element, it triggers there - target phase And finally it bubbles up to target’s ancestors - bubbling phase (will stop bubbling if any ancestors stopPropagation

So React is giving you the capture phase by appending it to onKeyUp which is onKeyUpCapture

Example of 3 different propagation phases

like image 52
Matin Kajabadi Avatar answered Oct 17 '22 05:10

Matin Kajabadi