Skip to content

Commit 37c8fee

Browse files
committed
fix(devtools): guard useLayoutEffect against null ref in ContextMenu
The useLayoutEffect in ContextMenu accesses ref.current without checking for null. When portalContainer is missing or items is empty, the component returns null (no portal rendered), leaving ref.current as null and causing a crash on the subsequent .contains() call. Guard the effect with the same early-return condition used by the render path (portalContainer == null || items.length === 0) so the effect is a no-op when no portal is mounted.
1 parent e49335e commit 37c8fee

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

packages/react-devtools-shared/src/devtools/ContextMenu/ContextMenu.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ export default function ContextMenu({
7474
);
7575

7676
useLayoutEffect(() => {
77-
const menu = ((ref.current: any): HTMLElement);
77+
const menu = ref.current;
78+
79+
// Match the early-return condition below. If neither of these
80+
// is true, menu being null would be a bug.
81+
if (portalContainer == null || items.length === 0) {
82+
return;
83+
}
7884

7985
function hideUnlessContains(event: Event) {
8086
if (!menu.contains(((event.target: any): Node))) {

0 commit comments

Comments
 (0)