Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 21 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20263 min read

Mastering Event Bubbling and Propagation in React

Learn how event bubbling works in React, how to stop unwanted propagation, and how to manage global vs. local events in your movie-browser app.

ReactDOMEventsJavaScriptFrontendWeb Development

Previously in this course, we looked at Handling Click Events: Interactive Components in React. While we successfully triggered functions, we didn't account for what happens when events overlap. Today, we’re digging into how events travel through the DOM and how you can control that flow.

Understanding Event Propagation

When you click a button inside a div, you aren't just clicking the button. You are clicking the button, its parent div, the section wrapping that, and so on, all the way up to the document object. This process is called event propagation.

In the browser, this happens in three phases:

  1. Capturing Phase: The event travels down from the window to the target element.
  2. Target Phase: The event hits the specific element you clicked.
  3. Bubbling Phase: The event "bubbles" back up from the target to the root.

React primarily hooks into the bubbling phase. When you attach an onClick to a component, React listens for that event as it bubbles up to the root of your application. Understanding this is critical because, in a complex UI like our movie browser, you might have a "Favorite" button inside a "Movie Card" that is itself clickable.

Why Bubbling Matters

Imagine your MovieCard has an onClick handler that opens the movie details, and inside that card, you have a "Delete" button. If you click the "Delete" button, you don't want the movie details to open—you only want the delete action to fire.

Without intervention, the click on the "Delete" button bubbles up to the MovieCard, triggering both events. This is a classic source of bugs in UI development.

Stopping Propagation

To prevent an event from moving further up the tree, you use the stopPropagation() method available on the event object.

Let’s update our MovieCard component to handle this interaction cleanly:

JSX
function MovieCard({ title, onDelete, onClick }) {
  const handleInternalClick = (e) => {
    // Stop the event from reaching the MovieCard's onClick
    e.stopPropagation();
    onDelete();
  };

  return (
    <div className="movie-card" onClick={onClick}>
      <h3>{title}</h3>
      <button onClick={handleInternalClick}>Delete Movie</button>
    </div>
  );
}

By calling e.stopPropagation(), we tell the browser: "The event stops here. Do not notify any parent components."

Hands-on Exercise: The "Click-Away" Challenge

In our movie browser project, let's implement a feature where clicking anywhere on the screen (except the search bar) clears the current search results.

  1. Create a GlobalClickListener component or add an onClick to your main App container.
  2. In your SearchBar component, ensure that clicking the input field does not trigger the global click listener that clears the search.
  3. Use e.stopPropagation() inside the search input's click handler to prevent the bubbling that would trigger the container's reset logic.

Common Pitfalls

  • Overusing stopPropagation(): Don't use it unless you have to. It makes your code harder to debug because it breaks the expected flow of events for other developers (or even analytics tracking scripts) listening at the document level.
  • Confusing preventDefault() and stopPropagation():
    • preventDefault() stops the browser's default behavior (like a form submitting or a link navigating).
    • stopPropagation() stops the event from traveling to parent elements.
  • React's SyntheticEvent: Remember that the event object you receive in React is a SyntheticEvent—a cross-browser wrapper around the native browser event. It behaves like the native event, but it's important to know it's a React-specific implementation. For more on how these event systems interact with the browser, check out our guide on Preventing DOM-based XSS: A Guide for Modern JavaScript Apps to see why controlling event sinks is a security best practice.

Recap

Event bubbling is the default behavior where events propagate from child to parent. In React, we manage this flow using the stopPropagation() method on the event object. Use this tool sparingly to ensure your UI interactions are predictable, especially when nesting clickable elements within your movie cards.

Up next: We'll take this knowledge of events and state to create a more complex UI interaction: Building a Movie Filter Toggle.

Previous lessonControlled ComponentsNext lesson Building a Movie Filter Toggle
Back to Blog

Similar Posts

ReactReactJune 24, 20263 min read

Setting Up with Vite: A Professional React Development Environment

Learn how to initialize a professional React project using Vite. We’ll cover the project directory structure and how to launch your fast development environment.

Read more
ReactReactJune 25, 2026

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 21 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min
4 min read

Mastering useEffect Dependencies: Control Your React Lifecycle

Learn to master the useEffect dependency array to control exactly when your side effects run. Avoid infinite loops and optimize your React components today.

Read more
ReactJune 25, 20264 min read

Handling Click Events: Interactive Components in React

Master React's onClick handler to make your app interactive. Learn how to trigger functions on user actions and pass event logic down to child components.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    3 min
  • 6

    Styling Components

    3 min
  • 7

    Passing Data with Props

    3 min
  • 8

    Dynamic Movie Cards

    3 min
  • 9

    Component Composition

    3 min
  • 10

    Conditional Rendering

    3 min
  • 11

    Rendering Lists of Data

    4 min
  • 12

    The Key Prop Explained

    4 min
  • 13

    Introduction to React State

    4 min
  • 14

    Managing State with useState

    3 min
  • 15

    Building an Interactive Search Bar

    3 min
  • 16

    Handling Click Events

    4 min
  • 17

    Updating State Based on Previous State

    4 min
  • 18

    Filtering the Movie List

    3 min
  • 19

    Handling Form Submissions

    3 min
  • 20

    Controlled Components

    4 min
  • 21

    Event Bubbling and Propagation

    3 min
  • 22

    Building a Movie Filter Toggle

    3 min
  • 23

    Introduction to Side Effects

    4 min
  • 24

    useEffect Dependencies

    4 min
  • 25

    Fetching Data from an API

    3 min
  • 26

    Handling Loading States

    3 min
  • 27

    Managing Errors

    3 min
  • 28

    Cleanup Functions in useEffect

    3 min
  • 29

    Debouncing Search Input

    3 min
  • 30

    Refactoring for Clean Code

    3 min
  • 31

    Folder Structure Best Practices

    4 min
  • 32

    Extracting Custom Hooks

    3 min
  • 33

    Prop Drilling and Context API

    3 min
  • 34

    Polishing the UI

    Coming soon
  • 35

    Finalizing the Movie Browser

    Coming soon
  • 36

    Review of Component Lifecycle

    Coming soon
  • 37

    Review of State Management

    Coming soon
  • 38

    Building a Modal Component

    Coming soon
  • 39

    Introduction to PropTypes

    Coming soon
  • 40

    Performance Optimization Basics

    Coming soon
  • 41

    Handling Browser History

    Coming soon
  • 42

    Working with LocalStorage

    Coming soon
  • 43

    Building a Favorites List

    Coming soon
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course