Skip to content

[DevTools] Fix null ref crash in ContextMenu when items list is empty#35929

Open
fresh3nough wants to merge 1 commit intofacebook:mainfrom
fresh3nough:fix/devtools-contextmenu-null-ref-crash
Open

[DevTools] Fix null ref crash in ContextMenu when items list is empty#35929
fresh3nough wants to merge 1 commit intofacebook:mainfrom
fresh3nough:fix/devtools-contextmenu-null-ref-crash

Conversation

@fresh3nough
Copy link

Summary

Fixes #35923

When the ContextMenu component renders with an empty items list (or when the portal container is missing), the component returns null and the portal <div ref={ref}> is never mounted. However, the useLayoutEffect still fires and calls repositionToFit(ref.current, ...) where ref.current is null, causing:

TypeError: Cannot read properties of null (reading 'ownerDocument')

This happens in the Timeline profiler view when the user right-clicks on the canvas before hovering over any specific event, resulting in an empty context menu items list being passed to the ContextMenu component.

How did you test this change?

Added contextMenu-test.js with two test cases:

  1. Verifies the component does not crash when items is empty (reproduces the exact error from the issue)
  2. Verifies the component renders correctly when items are provided

Steps to reproduce the original bug:

  1. Open React DevTools
  2. Record a profiling session
  3. Switch to the Timeline tab
  4. Right-click on the canvas area (not on a specific event)

With this fix, the right-click on an empty area no longer crashes.

@meta-cla meta-cla bot added the CLA Signed label Feb 28, 2026
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.
@fresh3nough fresh3nough force-pushed the fix/devtools-contextmenu-null-ref-crash branch from 29f83df to 37c8fee Compare February 28, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DevTools Bug] Cannot read properties of null (reading 'ownerDocument')

2 participants