Sub topic 9
Bubbling and capture
This chapter assumes you can attach handlers such as onClick, read the event object, and write components with props. Here you will learn the path an event takes through nested elements, and which handlers run in which order.
The three phases of an event
When a user clicks an element, the event doesn't only reach that element. It travels through the element's ancestors in three phases:
- Capture phase. The event moves down from the root to the parent of the target.
- Target phase. The event reaches the element that was clicked.
- Bubble phase. The event moves back up from the target to the root.
Normal handlers like onClick run in the bubble phase. This is why a click on a button also triggers an onClick on a surrounding div.
Seeing the order
Each event prop has a capture variant that ends in Capture: onClickCapture, onKeyDownCapture, onFocusCapture, and so on. The following component logs the order in which handlers run.
function Nested() {
return (
<div
onClickCapture={() => console.log('1 div capture')}
onClick={() => console.log('4 div bubble')}
>
<button
onClickCapture={() => console.log('2 button capture')}
onClick={() => console.log('3 button bubble')}
>
Click me
</button>
</div>
);
}Clicking the button logs the numbers in order: 1, 2, 3, 4. Capture handlers run from the outside in. Bubble handlers run from the inside out.
target and currentTarget
During bubbling, the same event object is passed to every handler on the path. Two properties tell you where you are:
event.targetis the element where the event started. It is the same for every handler on the path.event.currentTargetis the element whose handler is running right now.
function Panel() {
function handleClick(e) {
console.log('target:', e.target.tagName);
console.log('currentTarget:', e.currentTarget.tagName);
}
return (
<div onClick={handleClick}>
<button>Save</button>
</div>
);
}Clicking the button logs target: BUTTON and currentTarget: DIV. The handler belongs to the div, but the click began on the button. A parent can use target to handle clicks from many children with one handler.
When to use capture
Most code only needs bubble handlers. Use a capture handler when a parent must see an event before the child does. Two common cases are:
- Logging or analytics that should record every click, even if a child handles it.
- Intercepting an event before a child's handler runs, for example to block interaction while a section is disabled.
Common errors
- Expecting a parent handler not to run. A parent
onClickfires for clicks on any child. If a child action should not trigger the parent, you have to stop propagation. The next chapter covers that. - Reading
currentTargetafter anawait. React resetscurrentTargetonce the handler returns. Copy the value you need into a variable before any asynchronous work. - Mixing up the order. Capture handlers always run before bubble handlers for the same event, no matter where in the tree they are.
Summary
An event travels down through capture handlers, reaches the target, then travels back up through bubble handlers. onClick runs on the way up, and onClickCapture runs on the way down. Use target to find where the event started and currentTarget to find which handler is running.
Exercise
A button is inside a div. Both have an onClick handler, and the div also has an onClickCapture handler. In what order do the three handlers run when the button is clicked?
Show answer
First the div's onClickCapture runs on the way down. Then the button's onClick runs at the target. Last, the div's onClick runs as the event bubbles up.
Exercise
In a handler on a parent div, you click a child span. Which of event.target and event.currentTarget refers to the span?
Show answer
event.target refers to the span, because that is where the event started. event.currentTarget refers to the div, because that is the element the running handler is attached to.