MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Event Bubbling Through Portals Explained0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
11.1Breaking Out of DOM Hierarchies with createPortal11.2Building Modal Dialogs and Overlays with Portals11.3Event Bubbling Through Portals Explained
HomeTutorialsReact.js for IntermediateEvent Bubbling Through Portals Explained
Component Composition as an Alternative to Context

Event Bubbling Through Portals Explained

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The Surprising Portal Event Behavior

One of the most fascinating aspects of React Portals is how events behave:

Even though a portal is mounted into a completely different DOM node in the physical HTML, events fired inside the portal bubble UP through the REACT COMPONENT TREE, NOT through the physical HTML DOM tree!

Consider this scenario:

1React Component Tree:
2<ParentDiv onClick={handleParentClick}>
3 <ChildWithPortal>
4 └─ createPortal(<button>Click</button>, document.body)

In the physical HTML DOM:

  • The <button> is a direct child of <body>.
  • The <ParentDiv> is located deep inside <div id="root">.
  • Physically, <button> is NOT inside <ParentDiv>.

Yet, if you click the <button>: handleParentClick on <ParentDiv> WILL FIRE!

1Physical HTML DOM:
2<body>
3 <div id="root"> ──> <ParentDiv> (Not an ancestor of button!) </div>
4 <button id="portal-btn">Click Me</button>
5</body>
6 
7React Virtual DOM Event Flow:
8[button clicked] ════(Bubbles through React Tree!)════► [ParentDiv handleParentClick fires!]

2. Why Does This Happen?

React does not rely on native DOM event listeners attached to individual elements.

As explored in our SyntheticEvent architecture lessons:

  • React attaches unified listeners at the root of the React tree (<div id="root">).
  • React's synthetic event dispatcher traverses the Virtual DOM component hierarchy, not the browser's physical DOM tree.
  • Because <ChildWithPortal> is a logical child of <ParentDiv> in the React tree, React bubbles the SyntheticEvent up through the component hierarchy as expected!

3. Practical Demonstration

1import React, { useState } from 'react';
2import { createPortal } from 'react-dom';
3 
4function PortalModal({ onClose }) {
5 return createPortal(
6 <div className="portal-dialog">
7 <p>Inside Portal (Physical child of document.body)</p>
8 <button className="btn-action">Action Button</button>
9 </div>,
10 document.body
11 );
12}
13 
14export default function Container() {
15 const [clickCount, setClickCount] = useState(0);
16 
17 // This handler will catch clicks from inside the portal!
18 const handleContainerClick = () => {
19 setClickCount(prev => prev + 1);
20 };
21 
22 return (
23 <div className="parent-box" onClick={handleContainerClick}>
24 <h3>Parent Container (Clicks captured: {clickCount})</h3>
25 <p>Clicking the button inside the portal WILL increment this count!</p>
26
27 <PortalModal />
28 </div>
29 );
30}

4. How to Prevent Unintended Bubbling

If your portal represents a modal or tooltip and you do not want its internal click events to trigger parent container handlers:

  • Call e.stopPropagation() inside your portal event handler!
1// Stop event from bubbling up the React component tree:
2<div onClick={(e) => e.stopPropagation()}>
3 {/* Children inside this container will NOT trigger parent handlers */}
4</div>

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
How do SyntheticEvents bubble when an element is rendered through createPortal?
Question 2
If <Parent onClick={handleClick}> renders a portal that attaches a <button> to document.body, what happens when that button is clicked?
Question 3
Why does React implement event bubbling based on the React tree rather than the DOM tree?
Question 4
How can an engineer stop a portal's click events from reaching ancestor React components?
Question 5
If a native browser DOM event listener is attached via document.addEventListener('click'), will it receive portal clicks?
Next Lesson

Component Composition as an Alternative to Context

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Building Modal Dialogs and Overlays with PortalsComponent Composition as an Alternative to Context

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext