Sub topic 6
Events bubble through the React tree
This chapter assumes you know function components, props and children, useState and useEffect, and how DOM events bubble. It also assumes you have seen createPortal, which renders children into a different DOM node.
Two bubbling paths
A portal puts its children in a different place in the DOM than in the React tree. That raises a question: when you click inside portal content, which ancestors see the event?
React answers by following the React tree. A synthetic event (React's wrapper around the native browser event) bubbles from the portal content to the component that rendered the portal, and then up through that component's ancestors. The portal's DOM location does not matter for React handlers.
import { createPortal } from 'react-dom';
function Parent() {
return (
<div onClick={() => console.log('Parent received the click')}>
<PortalButton />
</div>
);
}
function PortalButton() {
return createPortal(<button>Click me</button>, document.body);
}The button is a child of document.body in the DOM, not of the div. Clicking it still logs Parent received the click, because PortalButton is a child of Parent in the React tree.
Why this is useful
This behavior lets a component treat portal content as part of itself. A parent can handle clicks from a modal it opened, track focus changes, or log analytics, without knowing that the modal lives somewhere else in the DOM.
Why it can surprise you
The same behavior causes bugs when a parent has a handler that should only run for its own DOM content. Common cases:
- A card with
onClickthat navigates to a detail page. A menu rendered through a portal from inside the card now triggers navigation when you click a menu item. - A row with
onClickthat toggles selection. A confirmation dialog opened from the row toggles it when you click Confirm. - A
onMouseEnteroronFocushandler on a parent that fires for portal children, even though the pointer or focus is visually elsewhere.
Stopping propagation
You have two options. The first is to stop the event inside the portal content, as the modal example does with e.stopPropagation(). The second is to filter in the parent, checking whether the event target is inside the parent's own DOM node.
import { useRef } from 'react';
import { createPortal } from 'react-dom';
function Card({ onOpen }) {
const cardRef = useRef(null);
function handleClick(event) {
if (!cardRef.current.contains(event.target)) return;
onOpen();
}
return (
<div ref={cardRef} onClick={handleClick}>
Card body
{createPortal(<button>Menu item</button>, document.body)}
</div>
);
}contains checks the real DOM. The portal button is not inside cardRef.current, so the handler returns early. Clicks on the card body still call onOpen.
Stopping propagation in the portal content is simpler, but it hides the event from every ancestor, including ones that may need it, such as a handler that closes other open menus. Filtering in the parent is more targeted. Pick based on who should see the event.
Native listeners follow the DOM
Handlers added with addEventListener use the DOM tree. A listener on a parent element will not see clicks from a portal rendered into document.body, because the DOM ancestors differ.
This matters for "click outside to close" logic. If you attach a listener to document and check whether event.target is inside your menu's ref, remember that a nested portal (for example a submenu) is outside that ref. A click in the submenu looks like an outside click.
| Handler type | Follows | Sees portal clicks from a descendant component? |
|---|---|---|
| React `onClick` on an ancestor component's element | React tree | Yes |
| `element.addEventListener` on that ancestor's DOM node | DOM tree | No |
| `document.addEventListener` | DOM tree | Yes, as every click reaches `document` |
Checklist
- Ask whether a parent handler should react to content that is rendered through a portal.
- If not, stop propagation in the portal content or filter with
contains. - For outside-click logic, treat portal descendants as inside, for example by checking each portal's ref.
Exercise
A <li onClick={select}> renders a Menu component that uses createPortal to place its items in document.body. Clicking a menu item also calls select. Why does this happen, and name one fix?
Show answer
React synthetic events bubble along the React tree, and Menu is a child of the li there, so the click reaches the li's onClick even though the items are in document.body in the DOM. Fixes: call event.stopPropagation() in the menu item's click handler, or have select return early when !liRef.current.contains(event.target).
Exercise
You add parentElement.addEventListener('click', log) to the DOM node of the Parent from the earlier example. Does clicking PortalButton call log? Why or why not?
Show answer
No. Native listeners follow the DOM tree, and the button is a child of document.body, not of the parent's DOM node, so the native click never passes through parentElement. Only React's onClick on Parent's div runs.