Understanding event bubbling in JavaScript and using it in React

How events climb up the DOM tree, how React manages them, and when and how to stop bubbling with stopPropagation – including a modal example.

Technological illustration of an event flow

Introduction

One of the important foundations of browser-based programming is JavaScript’s Event System. Event Bubbling, in which events propagate from deep inside the DOM tree towards the parent elements, is a core principle that allows flexibility but requires careful design. In React this mechanism takes on additional meaning, as part of an event management system based on virtualization of the DOM.

In this article we will explore in depth the bubbling process, its advantages, its limitations, and the way it fits into the React environment. In addition, we will discuss the cases in which it is right to prevent bubbling, and how to do so efficiently.

What is Event Bubbling?

Bubbling is the situation in which an event that occurs on a particular element “climbs” up the DOM tree and triggers event listeners defined on that component’s parent elements. For example, if we click a button (<button>) placed inside a <div> box, both the button and the box may fire onClick events, if such listeners have been defined.

How does bubbling work?

When an event is fired, the browser works in two main phases:

  • The Capturing phase: the event travels down the DOM tree from the top element (window or document) until it reaches the element on which the event was fired.
  • The Bubbling phase: after the event is handled at the target (the target element), the event climbs back up the tree and triggers listeners on parent elements.

The event mechanism in React

React implements a virtual event mechanism that provides an additional level of efficiency. Instead of registering listeners directly on every element in the DOM, React maps all events to the main document and manages them as a central event pool. This mechanism:

  • Reduces the number of listeners that are allocated.
  • Contributes to efficiency and performance.
  • Provides convenient control over events through a uniform API.

Even so, React keeps the basic principles of Event Bubbling, so a deep understanding of how this can be put to use is required.

Uses of bubbling in React

Event delegation

Bubbling makes it easier to manage events for many elements. Instead of adding an event listener (onClick) to every component, you can add a listener to the parent component and handle the events of all the children. For example:

JSX
const List = () => {
  const handleClick = (e) => {
    console.log(`Clicked on: ${e.target.tagName}`);
  };

  return (
    <ul onClick={handleClick}>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 1  3</li>
    </ul>
  );
};

Here, a click on any of the elements in the list will be handled by the event listener of the <ul> element.

Performing centralized actions

Bubbling makes it possible to manage complex logic in a centralized way, so there is no need to write redundant code for each component separately.

When would we want to stop bubbling?

Bubbling is not always desirable, mainly when an event that occurs in a child should not affect the parent component. Common situations:

  • Dropdown menus: a click inside a dropdown menu should not close it.
  • Modals and dialogs: internal actions should not trigger global actions on the screen.
  • Managing focused interactions: when an interaction in a particular component needs to stay isolated.

Example: a modal with stopped bubbling

In many systems, a modal is an interactive part intended to display information or to receive an action from the user. Often the modal needs to close when the user clicks outside it, but it is important to make sure this does not happen if the user clicks something inside the modal itself.

How does it work in the example?

  • The modal backdrop: listens to onClick. When someone clicks it, it calls the closeModal function, which closes the modal.
  • The modal content: also has an onClick listener. Inside the function we call e.stopPropagation() to prevent the event from “bubbling” to the backdrop.

The actual result

  • A click on the backdrop: calls the closeModal function and closes the modal.
  • A click inside the content: stops the event from bubbling thanks to stopPropagation(). The content itself responds to the click, but the event does not “climb” to the backdrop’s listener, so the modal does not close.

Implementation code example

JSX
const Modal = ({ closeModal }) => {
  const handleInnerClick = (e) => {
    e.stopPropagation(); // Prevents the modal from closing
  };

  return (
    <div onClick={closeModal} style={{ background: "rgba(0,0,0,0.5)", position: "fixed", inset: 0 }}>
      <div onClick={handleInnerClick} style={{ background: "white", padding: "20px", margin: "auto" }}>
        Modal content
      </div>
    </div>
  );
};

Conclusions and applications

Bubbling is a powerful tool in JavaScript and React that makes it possible to manage events efficiently and in an organized way. However, using it incorrectly can lead to bugs and functional problems. Therefore, it is recommended to:

  • Use bubbling to manage global, efficient events.
  • Stop bubbling only when there is a clear need.
  • Use stopPropagation() carefully, while keeping the user experience natural.

In closing

Bubbling is not only a technical tool, but also a principle that makes it possible to design more modular and efficient systems. Understanding it in depth is an important step for any developer who wants to write clean, sophisticated code suited to complex needs.

Search and commands