[DevTools] Fix null ref crash in ContextMenu when items list is empty#635
[DevTools] Fix null ref crash in ContextMenu when items list is empty#635everettbu wants to merge 3 commits into
Conversation
Greptile SummaryFixes a
Confidence Score: 5/5
Important Files Changed
Last reviewed commit: b20e929 |
29f83df to
37c8fee
Compare
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.
37c8fee to
7cf772a
Compare
Additional Comments (1)
The effect's closure captures In practice this is safe here because |
|
Upstream PR was closed or merged. Code is synced via branch mirror. |
Mirror of facebook/react#35929
Original author: fresh3nough
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.