Sub topic 10
Stopping propagation
This chapter assumes you know how to attach handlers with onClick, how to read the event object, and how events bubble up from the target element to its ancestors. Here you will learn how to end that trip early with stopPropagation, when doing so is useful, and what it does not do.
What stopPropagation does
When you click a button inside a div, React runs the button's onClick first. It then runs the div's onClick, because the event bubbles. Calling e.stopPropagation() in a handler tells React not to call handlers on elements above the current one.
function Toolbar() {
return (
<div onClick={() => console.log('toolbar clicked')}>
<button
onClick={e => {
e.stopPropagation();
console.log('button clicked');
}}
>
Click me
</button>
</div>
);
}Clicking the button logs only button clicked. Without the call, the console would also show toolbar clicked.
When to use it
Two situations come up often.
- A clickable container with a control inside. A card opens a details page when clicked, but it also has a "Delete" button. Without
stopPropagation, clicking Delete would also open the page. - A modal overlay. Clicking the dark backdrop closes the modal, but clicking inside the dialog must not.
function Modal({ onClose, children }) {
return (
<div className="overlay" onClick={onClose}>
<div className="dialog" onClick={e => e.stopPropagation()}>
{children}
</div>
</div>
);
}The dialog's handler does nothing except end the event there, so the overlay's onClose never runs for clicks inside it.
An alternative: check the target
Stopping propagation affects every ancestor, including ones you did not write. A narrower option is to let the event bubble and have the outer handler ignore events it does not own. Inside a handler, e.target is the element that was clicked, and e.currentTarget is the element the handler is attached to.
function Overlay({ onClose, children }) {
function handleClick(e) {
if (e.target === e.currentTarget) {
onClose();
}
}
return (
<div className="overlay" onClick={handleClick}>
{children}
</div>
);
}Now onClose runs only when the click lands on the backdrop itself. No child needs to call stopPropagation.
Common mistakes
- Expecting it to cancel default behavior.
stopPropagationandpreventDefaultare independent. Stopping a click inside a link does not stop the browser from following it. Calle.preventDefault()for that. - Calling it in a capture handler by accident. A handler such as
onClickCaptureruns on the way down. If it callsstopPropagation, the event never reaches the target, so the target's ownonClickdoes not fire. - Breaking listeners elsewhere. Analytics code or an "outside click" detector on
documentmay depend on seeing every click. A ReactstopPropagationcall also stops the native event from reaching those ancestors, so those listeners silently stop working. - Expecting it to affect handlers on the same element. It only blocks elements above the current one. Other handlers on that element still run.
Summary of choices
| Goal | Approach |
|---|---|
| Child control should not trigger the parent's handler | `e.stopPropagation()` in the child |
| Parent should react only to clicks on itself | Compare `e.target` with `e.currentTarget` |
| Block the browser's default action | `e.preventDefault()` |
Exercise
A list row has onClick={openRow} and contains a "Star" button with onClick={toggleStar}. Clicking Star currently also opens the row. What is the smallest change that fixes this, and what could go wrong with it?
Show answer
Make toggleStar accept the event and call e.stopPropagation(). The row's openRow then no longer runs for clicks on the button. The risk is that any other ancestor or document-level click listener, such as analytics or an outside-click detector, also stops seeing those clicks.