[DevTools] Fix null ref crash in ContextMenu when items list is empty#35929
Open
fresh3nough wants to merge 1 commit intofacebook:mainfrom
Open
[DevTools] Fix null ref crash in ContextMenu when items list is empty#35929fresh3nough wants to merge 1 commit intofacebook:mainfrom
fresh3nough wants to merge 1 commit intofacebook:mainfrom
Conversation
eps1lon
reviewed
Feb 28, 2026
packages/react-devtools-shared/src/devtools/ContextMenu/ContextMenu.js
Outdated
Show resolved
Hide resolved
eps1lon
reviewed
Feb 28, 2026
packages/react-devtools-shared/src/__tests__/contextMenu-test.js
Outdated
Show resolved
Hide resolved
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.
29f83df to
37c8fee
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #35923
When the
ContextMenucomponent renders with an emptyitemslist (or when the portal container is missing), the component returnsnulland the portal<div ref={ref}>is never mounted. However, theuseLayoutEffectstill fires and callsrepositionToFit(ref.current, ...)whereref.currentisnull, causing: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
ContextMenucomponent.How did you test this change?
Added
contextMenu-test.jswith two test cases:itemsis empty (reproduces the exact error from the issue)Steps to reproduce the original bug:
With this fix, the right-click on an empty area no longer crashes.